Syntax for finding multiple attributes
I am using the following jQuery XPath to find any element with the attribute "pathname" (a variable that takes the page location from the URL).
var local_url = "*[url=" + pathname + "]";
However, I now want to find whether this variable exists in one of two element attributes: url or alturl. How would I phrase this in XPath syntax so I don't have to use an if/else statement? (Eg, find all attributes whe开发者_运维百科re url=pathname OR alturl = pathname.)
Thanks!
You can use a multiple selector to find elements with either attribute matching, for example:
$("[url=" + pathname + "], [alturl=" + pathname + "]")
How would I phrase this in XPath syntax so I don't have to use an if/else statement? (Eg, find all attributes where url=pathname OR alturl = pathname.)
This XPath expression:
*[@url='someString' or @alturl='someString']
selects all elements that are children of the current node and have an attribute url
with value 'someString'
or (non-exclusive) have an attribute alturl
with value 'someString'
.
I now want to find whether this variable exists in one of two element attributes
This is exactly the behavior of node set comparison:
*[(@url|@alturl)='someString']
精彩评论