jquery:如果没有链接,请将#hash添加到所有链接中?

mat*_*att 1 hash jquery add

嘿伙计们,下面的代码运行正常...我正在为#wpf-wrapper中的所有链接添加一个#wpf-wrapper哈希.

$('#wpf-wrapper a').live('click',function() {
    $(this).attr('href', $(this).attr('href') + "#wpf-wrapper");        
});
Run Code Online (Sandbox Code Playgroud)

但是,如果有一个已经有的链接,例如href="#"我不想再添加一个链接.为什么以下代码不起作用?

$('#wpf-wrapper a').not('#wpf-wrapper a[href="#"]').live('click',function() 
    $(this).attr('href', $(this).attr('href') + "#wpf-wrapper");        
});
Run Code Online (Sandbox Code Playgroud)

突然间我的链接都没有添加#wpf-wrapper?

hun*_*ter 5

这个选择器是错误的

$('#wpf-wrapper a').not('#wpf-wrapper a[href="#"]')
Run Code Online (Sandbox Code Playgroud)

应该是这个使用属性包含选择器和适当的:not()选择器

$('#wpf-wrapper a:not([href*="#"])')
Run Code Online (Sandbox Code Playgroud)