简体   繁体   中英

Fastest method to detect if there is a tag with attribute [itemtype='http://schema.org/Offer']

如果在当前页面中存在具有属性[itemtype='http://schema.org/Offer']的HTML标记,我希望以最快的方式使用Javascript进行检测。

if(document.querySelectorAll("*[itemtype='http://schema.org/Offer']").length){
    alert("It is there!");
}

Use Sizzle for IE 7 and below . It is the same CSS selector engine for jQuery, but without all those fancy stuff.

document.querySelectorAll = document.querySelectorAll || Sizzle;

Heres some relevant information regarding jQuery selector speeds . Obviously it looks like using an ID in your selector would be the fastest, but if you cant do that, limiting the scope to a specific group of tags would also help

..edit.. if all you need to do is select the elements, you can just use the sizzle engine. Its what jquery uses internally, and you wont have to worry about any jQuery bloat.

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