mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
31 lines
695 B
Go
31 lines
695 B
Go
package goquery
|
|
|
|
import (
|
|
"exp/html"
|
|
)
|
|
|
|
// Attr() gets the specified attribute's value for the first element in the Selection.
|
|
// To get the value for each element individually, use a looping construct such as Each() or Map() method.
|
|
func (this *Selection) Attr(attrName string) (val string, exists bool) {
|
|
if len(this.Nodes) == 0 {
|
|
return
|
|
}
|
|
return getAttributeValue(attrName, this.Nodes[0])
|
|
}
|
|
|
|
// Private function to get the specified attribute's value from a node.
|
|
func getAttributeValue(attrName string, n *html.Node) (val string, exists bool) {
|
|
if n == nil {
|
|
return
|
|
}
|
|
|
|
for _, a := range n.Attr {
|
|
if a.Key == attrName {
|
|
val = a.Val
|
|
exists = true
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|