我想使用jquery选择列表锚链接。虽然在控制台输出中显示的页面中不存在'list'链接,但似乎'click'仍然被触发。什么可能导致“列表”和“添加”触发? 我使用jQuery 1.10.2这个简单的代码:JQuery锚链接选择器问题
<!-- <a href="#list">List</a> -->
<a href="#delete">Delete</a>
<a href="#add">Add</a>
<script>
jQuery(document).ready(function($) {
if ($('a[href$="#list"]').length>0){
console.log('list found');
}else{
console.log('list not found');
}
function opentab(value){
console.log('opentab: ' + value);
//perform task here
}
$(document).on('click', 'a[href="#list"]', opentab('list'));
$(document).on('click', 'a[href="#add"]', opentab('add'));
});
</script>
控制台输出:
list not found
opentab: list
opentab: add
这里的jsfiddle链接:http://jsfiddle.net/2FHf6/