将事件应用于JQuery中新创建的Div

时间:2022-10-22 08:11:22

I m working in Jquery..Since i m new to JQuery .i m having a doubt tat i have created a DIV inside that DIv i have one label and a Textbox..What i m trying to do is on clicking on that Generated Div , i need to show some existing value... But when i m clicking my DIv i kept an alert to check is that click event working.But that click not working... So, please suggest would the new created Divs are not allowed to Event clicking????? My code is,

我在Jquery工作。因为我是JQuery的新手。我怀疑我在DIv中创建了一个DIV我有一个标签和一个文本框。我想要做的就是单击Generated Div,我需要显示一些现有的价值...但是当我点击我的DIV我保持警告检查是点击事件工作。但是点击不工作...所以,请建议新创建的Div不允许事件点击? ????我的代码是,

     $("<div id=field></div>").appendTo("#displayPanel");
   $("<label id=labelstr>untitled1</label>").appendTo("#displayPanel #field");
   $("<input id=inputstr type= 'text' ></input>").appendTo("#displayPanel #field");

 $("#displayPanel #field").click(function (){alert("div clicked");});

2 个解决方案

#1


There's nothing that would prevent a new div element from being clicked. And in fact, I would suggest that something else on the page is preventing it from working (maybe a duplicate ID?)

没有任何东西可以阻止点击新的div元素。事实上,我建议页面上的其他内容阻止它工作(可能是重复的ID?)

Your code works just fine in isolation.

你的代码可以单独使用。

#2


You want the jQuery live function

你想要jQuery live函数

It allows you to add events to elements that are loaded after the DOM finishes loading.

它允许您将事件添加到DOM完成加载后加载的元素。

#1


There's nothing that would prevent a new div element from being clicked. And in fact, I would suggest that something else on the page is preventing it from working (maybe a duplicate ID?)

没有任何东西可以阻止点击新的div元素。事实上,我建议页面上的其他内容阻止它工作(可能是重复的ID?)

Your code works just fine in isolation.

你的代码可以单独使用。

#2


You want the jQuery live function

你想要jQuery live函数

It allows you to add events to elements that are loaded after the DOM finishes loading.

它允许您将事件添加到DOM完成加载后加载的元素。