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

后端 未结 10 853
北恋
北恋 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:07
    $('a[href="http:google.com"]')
    
    0 讨论(0)
  • 2021-01-07 09:07

    You can do this without jQuery.

    var links = document.querySelectorAll('a[href*="example.com"]');
    
    0 讨论(0)
  • 2021-01-07 09:07

    You can do this natively with querySelectorAll if your users are on IE8+ or any other browser. This method returns an NodeList of matching elements.

    document.querySelectorAll('a[href="exact/value.html"]');    // exact match
    document.querySelectorAll('a[href*="partial/value.html"]'); // partial match
    document.querySelectorAll('a[href^="starts/with"]');        // href starts with
    document.querySelectorAll('a[href$=".html"]');              // href ends with
    
    0 讨论(0)
  • 2021-01-07 09:09

    jQuery has a lot of selectors. The one you want here is the attribute selector.

    $('a[href="example.com"')
    
    0 讨论(0)
  • 2021-01-07 09:16

    You can use an attribute selector:

    $('a[href="http://example.com"]')
    
    0 讨论(0)
  • 2021-01-07 09:16

    With JQuery attribute selector, you can do this :

    $('a[href="example.com"]')
    
    0 讨论(0)
提交回复
热议问题