Is it possible to grab a link by its href if it doesn't have a class or ID?

后端 未结 10 1042
北恋
北恋 2021-01-07 08:56

I\'m using someone else\'s app and want to change the innerHTML in between any < a>< /a> tag that has a certain href. But these links don\'t have a class or ID associa

10条回答
  •  庸人自扰
    2021-01-07 09:33

    Select all elements that have the example.com value in href attribute:

    Live Demo: http://jsfiddle.net/NTGQz/

    $('a[href*="example.com"]');
    

    You can also try this, just to be more specific and following the OP "ideal" answer:

    Live Demo: http://jsfiddle.net/ksZhZ/

    jQuery.fn.getElementsByHref = function(str){ return $('a[href*="' + str + '"]'); };
    
    $(document).ready(function(){        
       elems = $(this).getElementsByHref('example.com');
    });
    

提交回复
热议问题