我通过 onclick() 将(这个)参数传递给函数。在函数中,当我尝试获取表数据单元格的调用对象的“名称”属性时,它失败了。这是代码。
<button name="abcd" onclick="foo(this)">
Button
</button>
<table>
<tr>
<td name="XYZ" onclick="foo(this)">Click Me Also</td>
</tr>
</table>
<script type="text/javascript">
var foo = function(xparam)
{
// This one works for both
alert(xparam.innerHTML);
// This one doesn't work for Table Cell (though works for Button)
alert(xparam.name);
};
</script>
这是 JSFiddle http://jsfiddle.net/8cvBM/312/
最佳答案
name
可能不是所有元素的属性(根据 doc table
没有 name 属性),因此请尝试 getAttribute
alert(xparam.getAttribute("name"));
关于JavaScript - 在 onclick 函数中获取(this)参数的关联值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40041500/