jQuery:如何获取动态生成的元素的ID? [英] jQuery: How to get ID of dynamically generated element?
问题描述
以下是摘要:
我正在根据AJAX调用的结果动态创建一个表.要添加行,我使用了之后"功能,因为表中有某些行充当标题.
I am creating a table dynamically from the results of an AJAX call. To add the rows, I am using the "after" function, seeing as there are certain rows in the table that function as headers.
添加这些行后,我有一个要编辑的数量框. 我可以向其中添加一个事件,但是无法获取行的ID($(this).parent('tr').attr('id')不起作用-返回未定义.
After I add these rows, I have a quantity box that I would like to make editable. I am able to add an event to it, however I can't get the ID of my row ($(this).parent('tr').attr('id') doesn't work - it returns undefined.
我该如何获取解析DOM树后添加的元素的ID值?
How would I go about being able to get the value of the ID for an element that was added after the DOM tree was parsed?
推荐答案
parent('tr')
仅返回直接父级.要成为<tr>
的直接子代,this
应该是<td>
.但是.linkQuantite
似乎是一个<a>
元素.这绝对不是<tr>
的直接子代.您想使用 parents('tr')
或更好的
The parent('tr')
returns only the direct parent. To be a direct child of a <tr>
, the this
should be a <td>
. But the .linkQuantite
seems to be an <a>
element. This is definitely not a direct child of <tr>
. You would rather like to use parents('tr')
or better closest('tr')
for this. The key difference is that parents('tr')
returns all parents matching 'tr'
while closest('tr')
returns only the first parent matchnig 'tr'
.
这篇关于jQuery:如何获取动态生成的元素的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!