jQuery 克隆 api 不会克隆本机 javascript 事件
如果我使用“element.onclick = function() {...};”向元素添加本机事件处理程序语法,后来我使用 'true' 参数使用 jQuery 克隆 api 克隆元素,然后克隆的元素不会获得本机添加的事件处理程序。如果我上面使用的本机语法是必须的(所以请不要给我类似“use $(element).click(function(e){...});”的答案,我该如何解决它? “, ETC..)。
在代码中:
编辑:请检查jsfiddle: http://jsfiddle.net/86f96/
If I add a native event handler to an element using the "element.onclick = function() {...};" syntax, and later I clone the element with the jQuery clone api, using 'true' parameter, then the cloned element doesn't get the natively added event handler. How can I solve it, if the native syntax that I've used above is a must (so pls don't give me an answer like "use $(element).click(function(e){...});", etc..).
In code:
EDIT: pls check on jsfiddle: http://jsfiddle.net/86f96/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
同一页面上不能有 2 个 ID 为
btn
的元素。编辑:
我查看了 jsfiddle,遗憾的是,除了在附加
clone
后再次附加单击事件之外,我无法想出不同的解决方案编辑2:
这是我得到的最接近的解决方案,无需更改原生 JS
onclick
事件。因此,如果由于某种原因,您无法更改先前定义的函数,这将有所帮助:代码取自 JS fiddle。
You can't have 2 elements with the ID
btn
on the same page.EDIT:
I looked at the jsfiddle, and sadly I can't come up with a different solution, other than attaching the click event again, after appending the
clone
EDIT 2:
This is as close as I got, without changing the native JS
onclick
event. So if for some reason, you can't change the previously defined function, this would help:The code is taken from the JS fiddle.
append 和appendTo 不传递事件。有些人将其视为错误,有些人将其视为功能,可惜唯一的方法是使用 live() 或重新定义事件。
append and appendTo don't pass the events. Some see it as a bug, some see it as a feature, alas the only way around it is to use live() or redefine the events.