简体   繁体   English

jQuery使整个div可在ajax内容上点击

[英]jQuery make whole div clickable on ajax content

I've got a Wordpress website and posts are loading dynamically. 我有一个Wordpress网站,帖子动态加载。 I've added this to the php file for single post : 我已经将其添加到php文件中以发布单个帖子:

HTML 的HTML

<article>
    <div class="blog-item-holder">
        <div class="featured-image">
            <a href></a>
        </div> Conent
    </div>
</article>

JS 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" );

but after the content has loaded dynamically, next posts are without this script. 但是在动态加载内容之后,接下来的帖子将没有此脚本。 Is it a good idea to use jQuery .on() ? 使用jQuery .on()是个好主意吗? How this code should look like in order to work? 该代码应如何工作?

Working with on is a good idea. 使用on是一个好主意。 Something like this: 像这样:

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" );

});

This only works if window.location is not refreshing the page. 仅当window.location无法刷新页面时,此方法才有效。

Use JQuery .on() 使用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