2016-12-16 49 views
1

如果條件工作但其他條件不起作用。如何阻止鏈接,如果其href包含散列?

(function($){ 
    $(".single-product a").on("click", function(e){ 
    var link = $(".single-product a").attr("href");  
    if(link == "#"){ 
     e.preventDefault(); 
     }else{ 
     return true; 
     } 
    }); 

}(jQuery)); 
+0

你知道散列鏈接是如何工作的? – madalinivascu

+0

看看這個:http://stackoverflow.com/questions/298503/how-can-you-check-for-a-hash-in-a-url-using-javascript –

+0

你好!請接受我的回答。 –

回答

1

你試過嗎?使用道具對ATTR它可能是你的jQuery庫較高

$(document).on("click",".single-product a", function(e){ 
    var link = $.trim($(this).prop("href")); 
    (link == "#") ? e.preventDefault() : ''; 
}); 
1

隨着attribute contains selector [name*=」value」]

$('.single-product a[href*="#"]').on('click', function (e) { 
 
    e.preventDefault(); 
 
});
<div class="single-product"> 
 
    <a href="https://www.google.com#foo">With #</a> 
 
</div> 
 

 
<div class="single-product"> 
 
    <a href="https://www.google.com">Without #</a> 
 
</div> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

相關問題