简体   繁体   中英

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 where url=pathname OR alturl = pathname.)

Thanks!

您可以使用多重选择器查找具有属性匹配的元素,例如:

$("[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'] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM