首页 > 解决方案 > 内联函数按 id 调用自动元素

问题描述

我有一个好奇想问,为什么通过在onclick函数中直接插入id,比如调用表格呢?

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>

标签: javascript

解决方案


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

window.theid // or short theid

代替

document.getElementById('theid')

虽然不推荐这样做


推荐阅读