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

后端 未结 10 855
北恋
北恋 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:34

    You can use a DOM3-attribute-selector (jQuery doc) to get all elements that contain a certain text in their href attribute. It would look like

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

    However, that might not be what you actually want - not only urls to that domain might contain this string. You might do something like begins-with:

    $('a[href^="http://example.com"]')
    

    but to get an exact and possibly more complex match, you don't get around a custom filter:

    $('a[href]').filter( function() {
         return this.hostname == "example.com";
         // or check other properties of the anchor element
    })
    

提交回复
热议问题