jquery遍历新创建的元素 [英] jquery iterating through newly created elements
本文介绍了jquery遍历新创建的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在表中添加新行,并将它们保存到数据库中。
I am trying to add new rows in my table, and save them into DB.
首先,我使用.append()在表上追加行:
First, I use .append() to append rows on the table:
$("#tablename").append("<tr id='newRow'><td>newly added row</td></tr>");
追加功能正常。我的页面显示正确的结果。
The appending function works fine. My page displays the correct result.
但是,我无法选择
$("#newRow").each(function () { alert "it never reaches here!"; });
我猜这是因为元素是在加载DOM之后添加的。
有谁能告诉我如何迭代我所有新添加的元素?
I am guessing it is because the elements are added after the DOM is loaded. Can anyone please tell me how I can iterate through all my newly added elements?
谢谢。
推荐答案
对于这种情况你应该使用类
,一个ID必须是unqiue(它是无效的HTML,会给一些奇怪的行为否则),像这样:
You should use a class
for this case, an ID has to be unqiue (it's invalid HTML and will give some weird behavior otherwise), like this:
$("#tablename").append("<tr class='newRow'><td>newly added row</td></tr>");
$(".newRow").each(function () { alert "it never reaches here!"; });
这篇关于jquery遍历新创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文