[英]Bind click function of child href to parent div
<div class="parent">
<a href="#">download link</a>
</div>
$(".parent").bind("click", function(){
alert('test');
});
想綁定href click函數,以便在單擊.parent時,它會轉到嵌套鏈接的位置。
$(".parent").bind("click", function(){
window.location.href = $(".parent a").attr("href");
});
嘗試這個:
$(".parent").bind("click", function(){
alert($(this).children('a').attr('href'));
});
由於父元素中只有一個鏈接,如果鏈接完全填充父寬度和高度,則可以完全避免使用javascript:只需設置此CSS
.parent a {
display: block;
width : 100%; /* or set in px if parent has not width specified */
height : 100%; /* or set in px if parent has not height specified */
}
嘗試類似下面的內容,它應該工作:
$(".parent a").bind("click", function(){
alert('test');
});
然后你可以這樣做:
// Choose the a tag specifically
$(".parent").bind("click", function() {
alert($(this).find('a').attr('href'));
});
這應該夠了吧
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.