Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JS向表中添加类名或id_Javascript_Html_Dom_Setattribute - Fatal编程技术网

Javascript 使用JS向表中添加类名或id

Javascript 使用JS向表中添加类名或id,javascript,html,dom,setattribute,Javascript,Html,Dom,Setattribute,假设这段代码使用DOM()创建一个带有plainJavaScript的表: 使用向元素添加属性,如下所示: var td1 = document.createElement('td'); var td2 = document.createElement('td'); td1.setAttribute('class', 'className'); td2.setAttribute('class', 'className'); 希望这有帮助。使用向元素添加属性,如下所示: var td1 = d

假设这段代码使用DOM()创建一个带有plainJavaScript的表:

使用向元素添加属性,如下所示:

var td1 = document.createElement('td');
var td2 = document.createElement('td');

td1.setAttribute('class', 'className');
td2.setAttribute('class', 'className');
希望这有帮助。

使用向元素添加属性,如下所示:

var td1 = document.createElement('td');
var td2 = document.createElement('td');

td1.setAttribute('class', 'className');
td2.setAttribute('class', 'className');
希望这能有所帮助。

请执行以下操作:

table.setAttribute("id", "myId");
阅读:

使用相同的函数设置
,就像前面提到的@Zakaria一样。

执行以下操作:

table.setAttribute("id", "myId");
阅读:


使用相同的函数设置
,就像前面提到的@Zakaria一样。

不鼓励引用。结帐:)感谢@RahulDesai的介入。。(链接更新为
https://developer.mozilla.org
)不鼓励引用。结帐:)感谢@RahulDesai的介入。。(链接更新为
https://developer.mozilla.org
)如果您像我一样是引导程序迷,请添加with:
table.setAttribute('class','table')
如果您像我一样是引导程序迷,请添加with:
table.setAttribute('class','table')