如何更改“a href”链接但只有 jQuery 中选定的某些链接?

发布于 2024-09-26 20:41:17 字数 370 浏览 0 评论 0原文

我想更改“a href”链接上的行为,但仅限于具有名为 open 的自定义属性的链接,

例如

<a href="#" action="open">Link 1</a>
<a href="http://www.google.co.uk/"></a>

我只想改变第一个的行为。所以我做了一个:

jQuery('a[action|=open]').live('click', function(evt) {
      do something;
});

但什么也没发生。选择器选择了所有指定的元素,但不执行单击事件。我做错了什么?

I want to change the behaviour on a "a href" link but only on ones that have a custom made attribute called open

e.g.

<a href="#" action="open">Link 1</a>
<a href="http://www.google.co.uk/"></a>

I want to change only the behaviour of the first one. So I do a:

jQuery('a[action|=open]').live('click', function(evt) {
      do something;
});

but nothing happens. The selector selects all the specified elements but the click event is not exectued. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2024-10-03 20:41:18

您的代码可以工作,但并非所有浏览器都支持自定义属性。我建议遵循@Diodeus 的回答,并使用类。这个 jsFiddle 在 Chrome 中适用于我,但可能不适用于所有浏览器。

Your code will work, but not all browsers support custom attributes. I suggest following @Diodeus' answer, and using classes. This jsFiddle works for me in Chrome, but it may not in all browsers.

强者自强 2024-10-03 20:41:17

使用类名更容易做到这一点:

$('a.open').click(function(){...})

<a href="#" class="open">Link 1</a>

It's easier to do this with a class name:

$('a.open').click(function(){...})

<a href="#" class="open">Link 1</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文