jQuery的/ JavaScript的:按钮添加动态与JavaScript不工作 [英] Jquery/Javascript: Buttons added dynamically with javascript do not work

查看:104
本文介绍了jQuery的/ JavaScript的:按钮添加动态与JavaScript不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML code块:

I have a code chunk of html:

<div id="chunk-1" class="chunk">  
    <div class="chunkText">Text<div>  
    <button class="addChunk">Click Me</button>  
</div>

<script>

$(".addChunk").click(function(){create_chunk(this.parentNode)})

function create_chunk(after_this){
    $(after_this).after(chunk_html)
    var i = 0
    $("div.chunk").each(function(){$(this).attr('id', "chunk-" + i++)})
    }

</script>

现在,这个工作,但仅适用于在页面上静态渲染的.chunk。当我preSS按钮出现第二个大块,但按钮不起作用。如果我添加的HTML要呈现两个或多个块,每一件作品,但它会为块的按钮没有。我应该怎么办?

Now, this works, but only for the .chunk that is statically rendered on the page. When I press the button a second chunk appears, but that button does not work. If I add the html for two or more chunks to be rendered, each one works, but the buttons for the chunks it creates do not. What should I do?

推荐答案

在下面的行中的事件处理程序附加click事件的元素,当您添加处理程序的选择匹配。

The event handler in the below line attaches the click event to the element matching the selector when you add the handler.

$(".addChunk").click(function(){create_chunk(this.parentNode)})

您可以使用href=\"http://api.jquery.com/live/\" rel=\"nofollow\">现场处理来做到这一点的

you can use the live handler to do this. the following code will solve your problem

$(".addChunk").live('click'. function(){create_chunk(this.parentNode)});

这篇关于jQuery的/ JavaScript的:按钮添加动态与JavaScript不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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