gpt4 book ai didi

javascript - 如何修复 "Uncaught TypeError: Cannot read property ' click' of null"

转载 作者:行者123 更新时间:2023-12-02 23:40:24 24 4
gpt4 key购买 nike

我正在开发 Chrome 扩展程序并收到错误“Uncaught TypeError: Cannot read property 'click' of null”。它在控制台中工作,我确保关闭控制台。我正在尝试在下面的网站上运行它。

https://realtime.demo.sonicwall.com/main.html

popup.js

document.addEventListener('DOMContentLoaded', function () {
document.querySelector('button').addEventListener('click', int);
});
function int() {
var source = document.getElementById('j1_64_anchor').click;
}
var coll = document.getElementsByClassName("collapsible");
var i;

popup.html

<body>
<button class="collapsible">Interfaces</button>
<div class="content">
<button onclick="int()" class="collapsible">Interfaces</button>
</div>
</body>

最佳答案

在您的 html 中,没有 id 为 j1_64_anchor 的元素。因此,您的代码 document.getElementById('j1_64_anchor').click(); 将失败。

document.addEventListener('DOMContentLoaded', function () {

document.querySelector('button').addEventListener('click', int)
});

function int() {
console.log('calling');
var source = document.getElementById('j1_64_anchor').click();
}

var coll = document.getElementsByClassName("collapsible");
var i;
<button class="collapsible">Interfaces</button>
<div class="content" id="j1_64_anchor">
<button onclick="int()" class="collapsible">Interfaces</button>
</div>

关于javascript - 如何修复 "Uncaught TypeError: Cannot read property ' click' of null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56105542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com