简体   繁体   English

jQuery如何通过href属性查找超链接

[英]jQuery how to find hyperlink by href attribute

Am trying to find hyperlink by href attribute i tried but no success with it not sure what am doing wrong 我正在尝试通过我尝试过的href属性查找超链接,但无法成功,不确定是怎么做的

here is the html 这是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> 

You've misidentified the problem. 您误解了问题。

jQuery('a[href*="15628"]') is finding the element just fine (I haven't checked the others). jQuery('a[href*="15628"]')发现元素很好(我没有检查其他元素)。

However, the value you assign to the onclick property must be a function and you are assigning a string. 但是,您分配给onclick属性的值必须是一个函数,并且您正在分配一个字符串。

You should use the on() method to assign event handlers when you are using jQuery though. 但是,在使用jQuery时,应该使用on()方法分配事件处理程序。

You can do this several ways, but your binding of the event is incorrect. 您可以通过多种方式执行此操作,但是对事件的绑定不正确。 See these examples from your code and one I added. 请参阅您的代码中的这些示例以及我添加的示例。

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM