javascript - 如何以编程方式单击具有 tabindex、onclick 和 id 作为属性的 div

标签 javascript html

document.getElementById("-1").click()
 <div tabindex="-1" id="-1" onclick="console.log('yes1')"></div>
    

不知何故 document.getElementById("-1").click() 抛出错误无法读取 null 的属性“click”。

请帮忙。

最佳答案

你的代码可以工作。尝试看看你在哪里使用 click() 函数,看看它们是否抛出错误。

document.getElementById("-1").click();
<div tabindex="-1" id="-1" onclick="console.log('yes1')"></div>

关于javascript - 如何以编程方式单击具有 tabindex、onclick 和 id 作为属性的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40975565/

相关文章:

javascript - 将不同 DIV 中的数字相加

css - 表格#t01 与 HTML 中的表格#01

html - 为什么img :hover doesn't work?

javascript - 在 react 状态下用数组替换字符串时出错

javascript - 将变量传递给静态谷歌图像映射

javascript - 问 - React Native App 介绍中的路由

javascript - 带 props 的内联模板中的 Vuejs 组件槽

c# - Selenium : xpath following-sibling where siblings have more children

javascript - 循环执行 Grunt 任务

javascript - 是否有任何解决方法可以重新抛出异常并保留 Javascript 中的堆栈跟踪?