Javascript 检索使用innerHTML创建的按钮的名称
我正在尝试检索使用innerHtml创建的按钮的名称。我的问题很容易理解,也很容易解决。提前谢谢 这是我的代码,将列添加到动态表中:Javascript 检索使用innerHTML创建的按钮的名称,javascript,Javascript,我正在尝试检索使用innerHtml创建的按钮的名称。我的问题很容易理解,也很容易解决。提前谢谢 这是我的代码,将列添加到动态表中: colonne1.innerHTML = '<td align="center"><input id=' + button_id_is_id_plat+ ' type="button" name='+title+' value="Supprimer" onclick="DeletePlat('+button_id_is
colonne1.innerHTML = '<td align="center"><input id=' +
button_id_is_id_plat+ ' type="button" name='+title+' value="Supprimer"
onclick="DeletePlat('+button_id_is_id_plat+')"></td>';
包含变量title的名称是我希望在DeletePlat函数中检索的名称。以下是我尝试过但没有积极结果的内容:
var my_array=document.getElementById("plat_action"); //the id of the dynamic table
var longueur = arrayLignes.length;
while(i<longueur)
{
//I retrieve the cell which contains a string
var cellule = my_array.rows[i].cells[0];
//I retrieve the cell which contains the button that I've created with innerHTML
var cellule2 = my_array.rows[i].cells[1];
//Here, I want to retrieve the name of the button embedded into my "cellule2" variable
if (cellule.innerText.toString()==cellule2.innerText.toString())
{
//treatment...
}
}
我也尝试过cellule2.name.toString,但它似乎不是解决方案。下面是一个示例,说明如何从按钮html元素检索名称: let buttonElement=document.getElementByIdbutton; 让name=buttonElement.getAttribute'name'; console.logname
HTML要重新查看按钮的名称,无需调用toString函数:只需使用DOM元素的.name属性: console.logdocument.getElementByIdbutton.name; 按钮
请张贴一个你知道名字的帖子,即标题。只是将其作为DeletePlat的第二个参数,就像您使用按钮\u id\u is\u id\u plat一样?下一个问题是,既然你已经有了id,为什么你还需要这个名字:这是一个糟糕的方法,但除此之外,你似乎在试图解决一个你本来不应该遇到的问题。退一步告诉我们实际的目标是什么我假设它正在为一个表行运行一些函数,并且需要函数中的行数据我同意@ChrisG,这看起来像是一个错误。执行已绑定到按钮的函数时,不必查找按钮的名称。1.执行回调2时,您已经可以访问按钮本身。设置似乎很奇怪。这里的最终目标是什么?可能根本不需要在表中搜索名称。下面介绍如何在不编写HTML字符串或搜索元素的情况下执行此操作:toString是冗余的,是的,但它试图找到正确的元素,而不是确定它的名称。