我有一个Facebook自定义按钮共享代码。我收到此错误:
未捕获的TypeError:无法在控制台中设置null的属性“ onclick”。
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');
}
});
}
scrtipt隐含在html文档的末尾。
答案 0 :(得分:2)
错误出现在您的第一行:
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');
}
});
});
});
答案 1 :(得分:0)
您可以一次使用document.getElementById
获得单个元素。因此,您可以将id
个元素保留在数组中,并在其上附加事件监听器
['piggy-bank-fb-shear-btn', 'piggy-bank-fb-shear-btn-2'].forEach(function(item){
document.getElementById(item).onclick==//rest of code
})
答案 2 :(得分:0)
var clickfnc = function() {
}
document.getElementById('piggy-bank-fb-shear-btn').onclick =clickfnc
document.getElementById('piggy-bank-fb-shear-btn-2').onclick =clickfnc
答案 3 :(得分:0)
document.getElementById
只有一个参数,您需要在代码中进行更改:
document.getElementById('piggy-bank-fb-shear-btn').onclick = function;
document.getElementById('piggy-bank-fb-shear-btn-2').onclick = function;
// Maybe you will prefer to use Jquery which you can use:
$('#piggy-bank-fb-shear-btn, #piggy-bank-fb-shear-btn-2').on('click', function() {});
答案 4 :(得分:0)
getElementById()
仅支持一个参数,因此您只能返回一个元素。
所以要么像这样一个一个地获取元素:
document.getElementById('piggy-bank-fb-shear-btn').onclick = function() { // stuff }
document.getElementById('piggy-bank-fb-shear-btn-2').onclick = function() { // stuff }
,或者您通过querySelectorAll()
来执行此操作,它支持多个参数。
var elements = document.querySelector('#piggy-bank-fb-shear-btn, #piggy-bank-fb-shear-btn-2');
但是现在您不能只在查询末尾添加onclick
,因为querySelectorAll()
返回了多个元素,即所谓的NodeList
。因此,您需要遍历名为elements
的集合:
for (var i = 0; i < elements.length; i++) {
elements[i].onclick = function() { // stuff }
}