是否可以使用jQuery选择<a>href以"ABC"结尾的所有链接?
例如,如果我想找到此链接 <a href="http://server/page.aspx?id=ABC">
tva*_*son 1517
$('a[href$="ABC"]')...
Run Code Online (Sandbox Code Playgroud)
选择器文档可以在http://docs.jquery.com/Selectors找到
对于属性:
= is exactly equal
!= is not equal
^= is starts with
$= is ends with
*= is contains
~= is contains word
|= is starts with prefix (i.e., |= "prefix" matches "prefix-...")
Run Code Online (Sandbox Code Playgroud)
小智 19
$('a[href$="ABC"]:first').attr('title');
Run Code Online (Sandbox Code Playgroud)
这将返回第一个链接的标题,该链接具有以"ABC"结尾的URL.
小智 14
$("a[href*='id=ABC']").addClass('active_jquery_menu');
Run Code Online (Sandbox Code Playgroud)
如果您不想导入像 jQuery 这样的大库来完成这么简单的事情,您可以使用内置方法querySelectorAll。几乎所有用于 jQuery 的选择器字符串也可以与 DOM 方法一起使用:
const anchors = document.querySelectorAll('a[href$="ABC"]');
Run Code Online (Sandbox Code Playgroud)
或者,如果您知道只有一个匹配元素:
const anchor = document.querySelector('a[href$="ABC"]');
Run Code Online (Sandbox Code Playgroud)
如果您要搜索的值是字母数字,您通常可以省略属性值周围的引号,例如,在这里,您也可以使用
a[href$=ABC]
Run Code Online (Sandbox Code Playgroud)
但报价更灵活,一般更可靠。
| 归档时间: |
|
| 查看次数: |
446747 次 |
| 最近记录: |