以下代码段会在单击每个链接后对其应用#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"); });更多推荐
发布评论