使用.append(html)创建的jQuery元素不可用 [英] jQuery elements created using .append(html) not available

查看:44
本文介绍了使用.append(html)创建的jQuery元素不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下

<script>
    $(document).ready(function() {
        $(".mapLink").click(function(){
            pos = $(this).attr("id");
            alert(pos);
        });
    });
</script>

<a class="map" id="test">Test</a>

当我点击Test时,我会收到警报......很棒。
但我也有以下内容...

When I click on Test I get an alert...great. But I also have the following...

<script>
    $(document).ready(function() {
        $(".mapLink").click(function(){
            pos = $(this).attr("id");
            alert(pos);
        });
    });
    $(#emp").change(function(){
        $("#existingAttendTime").html('');
        $.ajax({
            url: "existingAttendTime.php?emp=" + SelValue + "&time=0",
            cache: false,
            success: function(html){
                $("#existingAttendTime").append(html);
            }
        });
    });
</script>

<a class="mapLink" id="test">Test</a>
<div id="existingAttendTime"></div>

当emp更改时,它会触发并从existingAttendTime.php获取结果并将其插入到div中,所以现在它看起来像......

When the emp changes it fires off and gets the results from existingAttendTime.php and inserts it into the div, so now it looks something like...

<a class="mapLink" id="test">Test</a>
<div id="existingAttendTime"><a class="mapLink" id="12345">Return Test</a></div>

单击测试会向我发出测试警报,但点击返回测试则不会给我任何信息。

Clicking on Test gets me the alert "test", but clicking on Return Test gets me nothing.

我做错了什么或我错过了什么?

What am I doing wrong or what am I missing?

推荐答案

你需要在实时模式下绑定您的点击处理程序,否则新添加的DOM节点将不会触发它:

You need to bind your click handler in 'live' mode, or else newly-added DOM nodes won't trigger it:

$(document).ready(function() {
    $(".mapLink").live("click", function(){
        pos = $(this).attr("id");
        alert(pos);
    });
});

以前有一个实时查询所需的插件,但是jQuery 1.3集成了它的有限版本< a href =http://docs.jquery.com/Events/live =nofollow noreferrer>进入核心。另请注意,只有一些事件类型有效; 更改提交等不会,因此您必须在附加的同一函数内显式附加处理程序DOM的新节点。

There used to be a plugin required for live queries, but jQuery 1.3 integrated a limited version of it into core. Also note that only some event types work; change, submit, etc. will not, so you would have to explicitly attach the handler inside the same function that appended the new node to the DOM.

这篇关于使用.append(html)创建的jQuery元素不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆