javascript 如何在javascript中的td中设置具有某些值的id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3342743/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:53:32  来源:igfitidea点击:

how to set an id with some value in of td in javascript

javascripthtml-table

提问by user403269

i m creating td in javascript what i want is this "<td id='abc'>"how can i set the td id in javascript i use setAtteribute()not working

我在 javascript 中创建 td 我想要的是我"<td id='abc'>"如何在 javascript 中设置 td id 我用setAtteribute()不工作

my code look like this

我的代码看起来像这样

var cell4 = row.insertCell(3);
             cell4.innerHTML = "Song";
     var cell5 = row.insertCell(4);
     cell5.setAttribute('id','example1'); 

i want to set id of the td with value example plz help

我想用值示例设置 td 的 id plz help

回答by Marco Mariani

cell5.id = 'example1'; // javascript

cell5.id = 'example1'; // javascript

I suppose we're talking about IE here? I don't know about "id", but..

我想我们在这里谈论的是IE?我不知道“id”,但是..

http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html

http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html

回答by Salil

Using javascript(w/o jquery)

使用javascript(不带 jquery)

        cell4.id="some_td_id"

for example

例如

        advTable=document.getElementById("advance_option");
        advRow = advTable.insertRow(advTable.rows.length);
        advRow.id ="tr_id"  // Here you can set tr's id
        cell0=advRow.insertCell(0);
        cell0.id="td_id"  // Here you can set td's id
    cell1=advRow.insertCell(1);

回答by Chris

cell5.attr("id","example1"); //jquery