jquery:不是选择器问题?(jquery: not selector problem? [closed])

编程入门 行业动态 更新时间:2024-10-25 22:30:47
jquery:不是选择器问题?(jquery: not selector problem? [closed])

以下代码段会在单击每个链接后对其应用#breadcrumb哈希值。 这很好。

$('#main a').live('click',function() { $(this).attr('href', $(this).attr('href') + "#breadcrumbs"); });

现在我想确保只要链接中没有#hash就会发生这种情况。 否则会发生什么,我点击一个链接,结果如下: http://page.com/whatever#hash#breadcrumbs : http://page.com/whatever#hash#breadcrumbs我只是想阻止它。

但是,以下代码不起作用。 如果我添加:not selector没有链接添加#breadcrumb哈希(有或没有已存在的#hash)

$('#main a:not([href*="#"]').live('click',function() { $(this).attr('href', $(this).attr('href') + "#breadcrumbs"); });

知道我在这里做错了吗?

The following snippet applies a #breadcrumb hash to each link once it's clicked. That works fine.

$('#main a').live('click',function() { $(this).attr('href', $(this).attr('href') + "#breadcrumbs"); });

Now I want to make sure that happens just if a link does not already have a #hash in it. Otherwise what happens is I click a link and the outcome looks like this: http://page.com/whatever#hash#breadcrumbs I simply want to prevent that.

However the following code does not work. If I add the :not selector none of the links adds the #breadcrumb hash (with or without already existing #hash)

$('#main a:not([href*="#"]').live('click',function() { $(this).attr('href', $(this).attr('href') + "#breadcrumbs"); });

Any idea what I'm doing wrong here?

最满意答案

你的:not selector缺少一个右括号。 它应该是:

$('#main a:not([href*="#"])').live('click',function() { $(this).attr('href', $(this).attr('href') + "#breadcrumbs"); });

Your :not selector is missing a closing parenthesis. It should be:

$('#main a:not([href*="#"])').live('click',function() { $(this).attr('href', $(this).attr('href') + "#breadcrumbs"); });

更多推荐

本文发布于:2023-07-15 21:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选择器   jquery   selector   closed   problem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!