javascript - 内联函数通过 id 调用自动元素

标签 javascript

我很好奇,为什么要通过在onclick函数中直接插入id,比如调用table呢?

function See(table){
  console.log(table); // how system catch automatic the element?
}
<table id='iamtable' border="1">
  <thead>
    <th>Column 1</th>
    <th>Column 2</th>
    <th>Column 3</th>
    <th></th>
  </thead>
  <tbody>
    <tr>
      <td>Value</td>
      <td>Value</td>
      <td>Value</td>
      <td><button onclick='See(iamtable);'>press me</button>
    </tr>
  </tbody>
</table>

最佳答案

为元素设置 id,在 window 对象中创建引用。你可以使用

window.theid // or short theid

而不是

document.getElementById('theid')

虽然不推荐这样做

关于javascript - 内联函数通过 id 调用自动元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67211561/

相关文章:

javascript - jQuery Collapser 隐藏或显示 DIV 内的内容

javascript - 将夜间叠加层添加到谷歌地图 api

javascript - 我如何监听已检查的事件 knockout js

javascript - 在 React 中渲染字符串内的 HTML 实体

javascript - TypeError 'thing' 未定义,带有 javascript 实例

javascript - 如何使图像同步加载?

javascript - Bootstrap 导航对齐

javascript - 当我单击元素的子元素时更改状态(+AngularJS)

asp.net - 尝试读取 XML 文件,但他总是一样

javascript - 如何使用 jQuery 在引号之间设置 HTML 文本的样式?