开发者

How can i get every "a" tag which "href" attribute contain word "youtube"?

I want to catch every a tag which href attribute contain word youtube. I need开发者_Go百科 to use jquery.


$('a[href*="youtube"]')

For more selectors, see the Selectors portion of the jQuery API.


Attribute Contains Selector:

$('a[href*="youtube"]');


You can always use "filter":

var allYoutubes = $('a').filter(function() { return /youtube/.test(this.href); });

You could use a fancier selector, but this is simple and clear and possibly faster, as the library doesn't need to do the work of figuring out what your selector means. It's a matter of taste mostly.


pretty simple...

$('a[href*="youtube"]')

http://api.jquery.com/attribute-contains-selector/ http://api.jquery.com/category/selectors/


These other answers are great; but in case you're interested it's not that difficult to do a pure JS (no jQuery) alternative

function getYouTubeLinks() {

    var links = document.getElementsByTagName("a");
    var ytlinks = [];
       for(var i=0,l=links.length;i<l;i++){
           if(links[i].href.replace("http://","").indexOf("youtube.com") === 0) {
            ytlinks.push(links[i]);  
           } 
        } 
   return ytlinks;
}

var youtube = getYouTubeLinks();
for(var i=0,l=youtube.length;i<l;i++){
    youtube[i].style.color = "pink";   
}

it'd be a good idea to make sure to strip out "www." and "https://" as well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜