[英]jQuery make whole div clickable on ajax content
我有一個Wordpress網站,帖子動態加載。 我已經將其添加到php文件中以發布單個帖子:
的HTML
<article>
<div class="blog-item-holder">
<div class="featured-image">
<a href></a>
</div> Conent
</div>
</article>
JS
jQuery(document).ready(function() {
$(".blog-item-holder").click(function(){
window.location=$(this).find("a:first").attr("href");
return false;
});
$(".blog-item-holder").css( "cursor", "pointer" );
但是在動態加載內容之后,接下來的帖子將沒有此腳本。 使用jQuery .on()
是個好主意嗎? 該代碼應如何工作?
使用on
是一個好主意。 像這樣:
jQuery(document).ready(function() {
$(document).on("click", ".blog-item-holder", function(){
window.location=$(this).find("a:first").attr("href");
return false;
});
$(".blog-item-holder").css( "cursor", "pointer" );
});
僅當window.location無法刷新頁面時,此方法才有效。
使用JQuery .on()
$(".blog-item-holder").on("click", function(){
window.location=$(this).find("a:first").attr("href");
return false;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.