向动态表添加ID [英] Adding ids to the dynamic table

查看:115
本文介绍了向动态表添加ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能告诉我如何将ID添加到动态生成的表中吗?现在,我可以通过元素的类名来获取元素的长度,而且我不知道如何动态添加元素


-SanSel

Can you tell me how would you add ids to a dynamically generated table. Right now I can get the length of the element by its class name and I don''t know how to append it on the fly


-SanSel

推荐答案

首先,如果您打算将元素属性id添加到一些自动生成的元素中,则可以随时这样做,我不确定这是您可以使用的最佳技术.首先,为什么在生成表时没有生成所需的ID?其次,您是否需要使用document.getElementById?那是你的目的吗?相反,您可以将对所需的所有元素的引用保留在内存中,并在以后使用.我希望您也知道,使用JavaScript,您可以随时将任何属性添加到任何对象.换句话说,当您自动生成某些元素并将其添加到DOM时,您可以在DOM外部将它们作为通用JavaScript对象进行引用.只是值得深思.

现在,如果您确实需要使用DOM元素的id属性,我建议您使用jQuery和方法.attr().请参阅:
http://api.jquery.com/attr/ [ http://jquery.com/ [ ^ ],
http://en.wikipedia.org/wiki/JQuery [
First of all, if you mean to add the element attribute id to some automatically generated elements, you can always do it, I''m not sure this is the best technique you can use. First of all, why did not you generate the required IDs when you generated the table? Secondly, do you need to use document.getElementById? Is that your purpose? Instead, you could keep the references to all elements you need in memory and use it later. I hope you also know that with JavaScript, you can add any property to any object at any time. In other words, as you auto-generate some elements and add them to DOM, you can reference them outside DOM as general-purpose JavaScript objects. Just some food for thought.

Now, if you really need to use the id attribute of a DOM element, I would advise to use jQuery and the method .attr(). Please see:
http://api.jquery.com/attr/[^],
http://jquery.com/[^],
http://en.wikipedia.org/wiki/JQuery[^].

Using jQuery really makes such things easy to implement and maintain; and the code looks very compact. Also, the library addresses browser compatibility problems. I highly recommend using this library when it comes to using JavaScript in browsers.

—SA


这篇关于向动态表添加ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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