gpt4 book ai didi

javascript - 我收到错误 Uncaught TypeError : Cannot set property 'onclick' of null

转载 作者:行者123 更新时间:2023-11-30 14:20:12 25 4
gpt4 key购买 nike

我有一个 Facebook 自定义按钮共享代码。我收到此错误:

Uncaught TypeError: Cannot set property 'onclick' of null in console.

    document.getElementById('piggy-bank-fb-shear-btn', 'piggy-bank-fb-shear-btn-2').onclick = function () {
FB.ui({
method: 'share',
display: 'popup',
app_id: '{827182594132839}',
href: window.fbSheareUrl,
}, function (response) {
debugger;
if (response && !response.error_message) {
var element = document.getElementById("attach-email");
element.style.display = "block";
document.getElementById('close','close2').click();
}
else {
var element = document.getElementById("attach-email");
document.getElementById('close', 'close2').click();
element.style.display = "block";
document.getElementById('share-poup-text').innerHTML = getLanguageWordByKey('PiggyBankShareUnsuccessfulPopUp');
element.classList.remove("mystyle");
document.getElementById('piggy-share-icon').classList.remove('email-success');
document.getElementById('piggy-share-icon').classList.add('email-failed');

}
});
}

脚本在html文档的末尾实现。

最佳答案

错误在你的第一行:

document.getElementById('piggy-bank-fb-shear-btn', 'piggy-bank-fb-shear-btn-2')

document.getElementById() 一次仅支持一个 ID。

如果您想要定位多个 ID,请使用 document.querySelectorAll(),这将返回具有指定 ID 的节点列表:
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

或者,如果可能的话,您还可以向元素添加一个通用类,并使用 document.getElementsByClassName() 定位该类,这将返回具有该类的节点列表:
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

以你的例子为例:

var buttons = document.querySelectorAll('#piggy-bank-fb-shear-btn, #piggy-bank-fb-shear-btn-2');

Array.from(buttons).forEach(button => {
button.addEventListener('click', function(event) {
FB.ui({
method: 'share',
display: 'popup',
app_id: '{827182594132839}',
href: window.fbSheareUrl,
}, function (response) {
debugger;
if (response && !response.error_message) {
var element = document.getElementById("attach-email");
element.style.display = "block";
document.getElementById('close','close2').click();
}
else {
var element = document.getElementById("attach-email");
document.getElementById('close', 'close2').click();
element.style.display = "block";
document.getElementById('share-poup-text').innerHTML = getLanguageWordByKey('PiggyBankShareUnsuccessfulPopUp');
element.classList.remove("mystyle");
document.getElementById('piggy-share-icon').classList.remove('email-success');
document.getElementById('piggy-share-icon').classList.add('email-failed');

}
});
});
});

关于javascript - 我收到错误 Uncaught TypeError : Cannot set property 'onclick' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52891503/

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