Javascript 如何使用javascript给div标签一个唯一的id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7407067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to give a div tag a unique id using javascript
提问by Tom
I'm able to make a div tag using the document.createElement('div')
我可以使用 document.createElement('div') 制作一个 div 标签
However i do not know how to give it a unique id.
但是我不知道如何给它一个唯一的 id。
can anyone help me with this.
谁能帮我这个。
I know how to do it using innerHTML however it very cumbersome. (I heard it not a good way of creating a layout.)
我知道如何使用 innerHTML 做到这一点,但它非常麻烦。(我听说这不是创建布局的好方法。)
回答by Joel A. Villarreal Bertoldi
Understanding unique
as an ID that must not get mixed up with any other ID's in the markup, the easiest way to go is to get the local timestamp. As shown here:
理解unique
为不能与标记中的任何其他 ID 混淆的 ID,最简单的方法是获取本地时间戳。如图所示在这里:
let div = document.createElement("div");
// With old JS syntax
div.id = "div_" + new Date().getTime().toString();
// With ES6 Template Strings you can also write
div.id = `div_ ${new Date().getTime().toString()}`;
Though working with createElement
can be a bit of a troublemaker, you should be using some JavaScript framework that solve the tiny little details for you (such as jQuery, Mootools, Dojo, etc.).
尽管使用createElement
可能有点麻烦,但您应该使用一些 JavaScript 框架来为您解决微小的细节(例如 jQuery、Mootools、Dojo 等)。
回答by Chris Baker
var d = document.createElement('div');
d.id = 'myElementId';
d.innerHTML = 'This is the content!';
Or
或者
var d = document.createElement('div')
.id = 'myElementId';
.. same thing really, just a different way of laying it out.
..同样的事情,只是一种不同的布局方式。
This takes care of assigning the id. Now, for unique. The best way is to use the current time, as it isn't likely to repeat since javascript time is on miliseconds.
这负责分配 id。现在,为了独一无二。最好的方法是使用当前时间,因为它不可能重复,因为 javascript 时间是毫秒。
Putting it together:
把它放在一起:
var d = document.createElement('div')
.id = 'id'+new Date().getTime().toString();
This does have the chance to duplicate itself in the right circumstances. If it is is hyper-critical to maintain uniqueness, then the only way is to use a pointer:
这确实有机会在适当的情况下自我复制。如果保持唯一性非常关键,那么唯一的方法就是使用指针:
// establish a variable to hold the pointer
var uniquePointer = 0;
// every time you use the pointer to create a new element, increment the pointer.
var d = document.createElement('div')
.id = 'id'+uniquePointer;
uniquePointer ++;
回答by mateusz.fiolka
You can use a library for this: https://github.com/LiosK/UUID.js
您可以为此使用一个库:https: //github.com/LiosK/UUID.js
It "Generates RFC 4122 compliant UUIDs."
它“生成符合 RFC 4122 的 UUID”。
Having the element you can assign it the id using the code:
拥有该元素,您可以使用以下代码为其分配 id:
element.id = "somePrefix" + UUID.generate()
回答by Jonathan M
var myDiv = document.createElement('div');
myDiv.id = "myUniqueId";