我试图通过我尝试过的href属性查找超链接,但不确定是否做错了
这是html
jQuery('a[href*="15628"]').prop("onclick", "window.location.href='https://stackoverflow.com/?page_id=15628';console.log('no ajax');return false;");
jQuery('a[href="https://stackoverflow.com/?page_id=15628"]').prop("onclick", "window.location.href='https://stackoverflow.com/?page_id=15628';console.log('no ajax');return false;");
jQuery('ul#menu-main_menu').find('a[href*="15628"]').prop("onclick", "window.location.href='https://stackoverflow.com/?page_id=15628';console.log('no ajax');return false;");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<nav class="main_menu drop_down right">
<ul id="menu-main_menu" class="">
<li id="nav-menu-item-15512" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home page_item page-item-15260 current_page_item narrow">
<a href="https://stackoverflow.com/" class="">
<i class="menu_icon blank fa" />
<span>Home</span>
<span class="plus" />
</a>
</li>
<li id="nav-menu-item-15685" class="menu-item menu-item-type-post_type menu-item-object-page narrow active">
<a href="https://stackoverflow.com/?page_id=15628" class="current">
<i class="menu_icon blank fa" />
<span>FAQs</span>
<span class="plus" />
</a>
</li>
</ul>
</nav>
<a href="https://stackoverflow.com/?page_id=15628" class="current">
<i class="menu_icon blank fa" />
<span>FAQs</span>
<span class="plus" />
</a>
答案 0 :(得分:3)
您误解了问题。
jQuery('a[href*="15628"]')
发现元素很好(我没有检查其他元素)。
但是,您分配给onclick
属性的值必须是函数,并且您正在分配字符串。
在使用jQuery时,应使用the on()
method分配事件处理程序。
答案 1 :(得分:1)
您可以通过多种方式执行此操作,但是您对事件的绑定不正确。从您的代码和我添加的代码中查看这些示例。
console.log(jQuery('a[href*="15628"]').length);
console.log(jQuery('a[href="https://stackoverflow.com/?page_id=15628"]').length);
jQuery('a[href="https://stackoverflow.com/"]').on("click", function(event) {
event.preventDefault();
event.stopPropagation();
console.log('home link');
// window.location.href = 'https://stackoverflow.com/?page_id=15628';
return false;
});
jQuery('a[href*="15628"]').on("click", function(event) {
event.preventDefault();
event.stopPropagation();
console.log('no ajax1', $(this).attr('href'));
// window.location.href = 'https://stackoverflow.com/?page_id=15628';
return false;
});
jQuery('a[href="https://stackoverflow.com/?page_id=15628"]').on("click", function(event) {
console.log('no ajax0', $(this).attr('href'));
event.preventDefault();
event.stopPropagation();
// window.location.href = 'https://stackoverflow.com/?page_id=15628';
return false;
});
jQuery('ul#menu-main_menu').find('a[href*="15628"]').on("click", function(event) {
event.preventDefault();
event.stopPropagation();
console.log('no ajax2', $(this).attr('href'));
// window.location.href = 'https://stackoverflow.com/?page_id=15628';
return false;
});
jQuery('ul#menu-main_menu').on("click", 'a[href*="15628"]', function(event) {
event.preventDefault();
event.stopPropagation();
console.log('no ajax3', $(this).attr('href'));
// window.location.href = 'https://stackoverflow.com/?page_id=15628';
return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<nav class="main_menu drop_down right">
<ul id="menu-main_menu" class="">
<li id="nav-menu-item-15512" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home page_item page-item-15260 current_page_item narrow">
<a href="https://stackoverflow.com/" class="">
<i class="menu_icon blank fa">
</i><span>Home</span><span class="plus">
</span></a>
</li>
<li id="nav-menu-item-15685" class="menu-item menu-item-type-post_type menu-item-object-page narrow active">
<a href="https://stackoverflow.com/?page_id=15628" class="current">
<i class="menu_icon blank fa"></i><span>FAQs</span><span class="plus"></span></a>
</li>
</ul>
</nav>
<a href="https://stackoverflow.com/?page_id=15628" class="current"><i class="menu_icon blank fa"></i><span>FAQs</span><span class="plus"></span></a>