简体   繁体   中英

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

here is the 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).

However, the value you assign to the onclick property must be a function and you are assigning a string.

You should use the on() method to assign event handlers when you are using jQuery though.

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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