简体   繁体   English

动态重新加载后的jQuery选择器和$(this)

[英]jQuery selectors after dynamic reload, and $(this)

I need some help. 我需要协助。 I load a list of entries in a div every 5 seconds. 我每5秒在div中加载一个条目列表。 Each entry is a div and has a unique ID. 每个条目都是一个div,并且具有唯一的ID。 Like this: 像这样:

<div class="entry">
<div class="textbox">
    <p class="entry-text">
        <?php echo $text;?>
    </p>
</div>
<div class="infobox">
    <p class="date"><a #<?php echo $id;?> id="<?php echo $id;?>" href="gen_details.php?id=<?php echo $id;?>"><?php echo $t;?></a> </p>
    <p class="ip"><?php echo $ip;?></p>
</div>

These, as I said are loaded each 5 seconds. 正如我所说,这些是每5秒加载一次。 I'm adding a details page for every entry, with this: 我为每个条目添加一个详细信息页面,其内容如下:

                $('.date a').click(function () {
                var dataString = 'id=' + $(this).attr("id"); 
                //alert(dataString);
                $.ajax({
                    type: "POST",
                    url: "gen_details.php",
                    data: dataString,
                    success: function(data) {
                        $("#content").hide().fadeOut('fast');
                        $("#content").html(data).show('fast');
                        refresh = 0;
                    },
                });
                return false;
            });

This works perfectly fine, until it reloads. 正常工作,直到重新加载。 It seems to lose the handle for the a href and instead of doing the procedure it goes to gen_details.php 它似乎失去了a href的句柄,而不是执行该过程,而是转到gen_details.php。

I have tried to use .on() but I don't know how would I get the ID of the entry using .on(), as I cant use $(this) (afaik). 我尝试使用.on(),但是我不知道如何使用.on()获得条目的ID,因为我不能使用$(this)(afaik)。

I hope I explained my problem at least half-well. 我希望至少能解释一半我的问题。 English is not my first language so it wasn't that easy. 英语不是我的母语,所以英语不是那么容易。

Thanks in advance. 提前致谢。

Live click event bind event handler to element even after you reload some element. Default click event bind to element when a page load once you reload that element then it also delete event handler of that element.
works on till jquery 1.7 version.

    $('.date a').live('click',(function (e) {
    e.preventDefault()
                    var dataString = 'id=' + $(this).attr("id"); 
                    //alert(dataString);
                    $.ajax({
                        type: "POST",
                        url: "gen_details.php",
                        data: dataString,
                        success: function(data) {
                            $("#content").hide().fadeOut('fast');
                            $("#content").html(data).show('fast');
                            refresh = 0;
                        },
                    });

                });
//when jQuery > 1.7 then used this method

$('body').on('click' '.date a',function () {
  //call 
 });

Try this selector 试试这个选择器

$('div').on('click', '.date a', function () {

This will delegate the event to its parent div. 这会将事件委托给其父div。 So it should work for dynamically created elements as well. 因此,它也应适用于动态创建的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM