[英]Id not being passed through function call JavaScript
I have a password vault being served from a server for a special page on our intranet at work, but I'm having issues passing the ID through the edit()
function into my script. 我有一个从服务器提供的密码仓库,用于工作中Intranet上的特殊页面,但在将ID通过
edit()
函数传递到脚本中时遇到问题。 Here is the applicable markup (as you can see it is contained in a PHP
array. Everything, including the id
is being served correctly (ie the ids at the source will read n1
or p7
etc...)): 这是适用的标记(如您所见,它包含在
PHP
数组中。包括id
在内的所有内容均已正确提供(即,源处的id读为n1
或p7
等)):
<tr id='$OTHERInfo[0]'>
<td id='n$OTHERInfo[0]' onclick='edit(this)' contentEditable='false'>$OTHERInfo[1]</td>
<td id='u$OTHERInfo[0]' onclick='edit(this)' contentEditable='false'>$OTHERInfo[2]</td>
<td id='p$OTHERInfo[0]' onclick='edit(this)' contentEditable='false'>$OTHERInfo[3]</td>
<td id='d$OTHERInfo[0]' onclick='edit(this)' contentEditable='false'>$OTHERInfo[4]</td></tr>
Here is the script...it's very simple (it is contained at the bottom of the document): 这是脚本...非常简单(包含在文档底部):
<script>
function edit(id){
document.getElementById(id).style.backgroundColor = "white";
document.getElementById(id).style.color = "black";
document.getElementById(id).setAttribute("contentEditable", "true");
}
</script>
In the console the error message reads: 在控制台中,错误消息显示为:
Uncaught TypeError: Cannot read property 'style' of null
So obviously the ID is not getting passed correctly, but I don't know what to do. 因此很明显,该ID未正确传递,但我不知道该怎么办。 I've also tried just trying to "hard code" the ID into the function in the
onclick
like this: edit(n$OTHERInfo[0])
but I have the same issue. 我也尝试过尝试将ID“硬编码”到
onclick
的函数中,如下所示: edit(n$OTHERInfo[0])
但我遇到了同样的问题。
'this' is the entire <td>
element not just its id, so your script should read: 'this'是整个
<td>
元素,而不仅仅是其ID,因此您的脚本应为:
<script>
function edit(element){
element.style.backgroundColor = "white";
element.style.color = "black";
element.setAttribute("contentEditable", "true");
}
</script>
id
at edit
function is a reference to the clicked element edit
功能处的id
是对clicked元素的引用
onclick='edit(this.id)'
or 要么
function edit(id){
id.style.backgroundColor = "white";
id.style.color = "black";
id.setAttribute("contentEditable", "true");
}
You are providing edit(this)
. 您正在提供
edit(this)
。 The keyword this
references the actual element. 关键字
this
引用的实际元素。 It doesn't reference the ID of the element. 它没有引用元素的ID。 You can revise your code without
document.getElementById
to work. 您可以在没有
document.getElementById
情况下修改代码。
function edit(element) {
element.style.backgroundColor = "white";
element.style.color = "black";
element.setAttribute("contentEditable", "true");
}
<td id='n$OTHERInfo[0]' onclick='edit(this)' contentEditable='false'>$OTHERInfo[1]</td>
If you pass this
to edit
function, you will use the entire element(ie the <td>
element) as the parameter rather than its id. 如果
this
传递给edit
函数,则将使用整个元素(即<td>
元素)作为参数,而不是其ID。
You could try onclick='edit(this.id)'
. 您可以尝试
onclick='edit(this.id)'
。 This will give you the id of the <td>
element as parameter. 这将为您提供
<td>
元素的id作为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.