为动态标签编写点击事件 [英] write click event for dynamic label

查看:50
本文介绍了为动态标签编写点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友



i每个点击按钮都有按钮我会动态创建一个标签,有相同的ID

,现在我怎么能为每个动态创建的标签写一个onclick事件,





提前谢谢

解决方案

我强烈建议使用jQuery,但是你应该更好地使用if来插入新标签,以获得已经包含jQuery的对象。请参阅:

http://api.jquery.com/category/操纵/ dom-insertion-around / [ ^ ],

http://api.jquery .com / category / manipulation / dom-insertion-inside / [ ^ ],

http: //api.jquery.com/category/manipulation/dom-insertion-outside/ [ ^ ]。



然后点击事件写成如下:

 myLabel.click( function ( ){
// 这里有一些处理程序代码
// 顺便说一句:
var sender =


this ); // 发件人对象的jQuery包装器
// 在您的情况下,您点击的标签相同
// 这样,您可以在处理程序中使用发件人的属性
});



请参阅: http://api.jquery.com/click [ ^ ]。



如果您需要学习jQuery(强烈推荐),请参阅:

http://en.wikipedia.org/wiki/JQuery [ ^ ],

http://jquery.com [ ^ ],

http://learn.jquery.com [ ^ ],

http:/ /learn.jquery.com/using-jquery-core [ ^ ],

http://learn.jquery .com / about-jquery / how-jquery-works [ ^ ](从这里开始)。



如果你不想听好建议并想写它在Javascript中你自己从头开始,你可以随时使用

< br /> 
< pre lang =Javascript>< br />
文件添加处理程序.getElementById(myLabel)。addEventListener(click,function(){alert(Hello World!); });< / pre>< br />
上面的示例是此处显示的示例组合:< a href =http://www.w3schools.com/js/js_htmldom_eventlistener.asp > http://www.w3schools.com/js/js_htmldom_eventlistener.asp< / a> [< a href =http://www.w3schools.com/js/js_htmldom_eventlistener.asp\"target =_ blanktitle =新窗口> ^< / a>]。< br />
< br />
< dd> -SA< / dd>


hi friends

i have button on each click button i will create a label dynamically,with same ids
,now how can i write a onclick event for the each dynamically created label ,


Thanks in advance

解决方案

I would strongly recommend using jQuery, but then you should better use if for inserting the new label as well, to get a already jQuery-wrapped object. Please see:
http://api.jquery.com/category/manipulation/dom-insertion-around/[^],
http://api.jquery.com/category/manipulation/dom-insertion-inside/[^],
http://api.jquery.com/category/manipulation/dom-insertion-outside/[^].

Then the click event is written like this:

myLabel.click(function() {
    // some handler code here
    // by the way:
    var sender =


(this); // the jQuery wrapper of the sender object // in your case the same label you clicked on // this way, you can use sender's properties in your handler });


Please see: http://api.jquery.com/click[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com[^],
http://learn.jquery.com[^],
http://learn.jquery.com/using-jquery-core[^],
http://learn.jquery.com/about-jquery/how-jquery-works[^] (start from here).

If you don't want to listen to a good advice and want to write it in Javascript by yourself from scratch, you can always add a handler by using

<br />
<pre lang="Javascript"><br />
document.getElementById("myLabel").addEventListener("click", function(){ alert("Hello World!"); });</pre><br />
The sample above is a combination of samples shown here: <a href="http://www.w3schools.com/js/js_htmldom_eventlistener.asp">http://www.w3schools.com/js/js_htmldom_eventlistener.asp</a>[<a href="http://www.w3schools.com/js/js_htmldom_eventlistener.asp" target="_blank" title="New Window">^</a>].<br />
<br />
<dd>—SA</dd>


这篇关于为动态标签编写点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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