繁体   English   中英

我似乎无法将值传递给 javascript 函数

[英]I can't seem to pass a value to javascript function

我的代码是这样的

 function show_file(itemid) { document.getElementById("itemid").innerHTML = "Testing"; }
 <td id='$itemid' class='header-row'> MyText quote&nbsp;&nbsp; <button onclick='show_file($itemid)'> Button<i class='fa fa-angle-down'></i> </button> </td>

但是我收到此错误“无法设置 null 的属性 'innerHTML'”,所以 $itemid 的值似乎没有通过?

您应该传递实际函数的参数itemid而不是字符串“itemid”

将其更改为:

function show_file(itemid) {
  document.getElementById("itemid").innerHTML = "Testing";
}

到:

function show_file(itemid) {
  document.getElementById(itemid).innerHTML = "Testing";
}

那是因为您在参数中为 getElementById 函数提供了一个字符串,而不是 itemid 变量。 尝试这个 :

function show_file(itemid) {
  document.getElementById(itemid).innerHTML = "Testing";
}

暂无
暂无

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

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