簡體   English   中英

jQuery使整個div可在ajax內容上點擊

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM