简体   繁体   English

如何给新创建的元素一个类名?

[英]How to give class name to a newly created element?

I am creating a new element like this: 我正在创建一个像这样的新元素:

var cell3=row.insertCell(2);
var t3=document.createElement("input");
t3.id = "txtEstStartDt"+index;
cell3.appendChild(t3);

As I have given an id to the element, I want to give a class. 因为我给元素提供了一个ID,所以我想给一个类。

t3.class = "abc";

Will this work? 这样行吗?

You need to use className 您需要使用className

t3.className = "abc";

Alternative in jQuery (since you tagged it such) would be jQuery中的替代方法(因为您已经标记了它)将是

var t3 = $("<input/>",{"id":"txtEstStartDt"+index,"class":"abc"});

or later: 或更高版本:

t3.addClass("abc");

but then it would be better to make it all jQuery. 但是最好还是全部使用jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM