开发者

Syntax for finding multiple attributes

开发者 https://www.devze.com 2023-01-28 10:15 出处:网络
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).

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'] 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号