且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将ID添加到动态创建的< div>

更新时间:2022-12-08 10:34:15

如果我找到你的话,那就像

If I got you correctly, it is as easy as

cartDiv.id = "someID";

不需要jQuery。

No need for jQuery.

查看属性DOM元素

对于类,它是相同的:

cartDiv.className = "classes here";

但请注意,这将覆盖现有的类名。如果要动态添加和删除类,则必须使用jQuery或编写自己的函数来执行某些字符串替换。

But note that this will overwrite already existing class names. If you want to add and remove classes dynamically, you either have to use jQuery or write your own function that does some string replacement.