JavaScript动态onClick问题
当页面上的某个内容发生变化时,我试图给按钮一个onclick事件。我尝试过很多不同的方法,但都没有成功。我做错了什么 下面是我尝试过的JavaScript动态onClick问题,javascript,dom,Javascript,Dom,当页面上的某个内容发生变化时,我试图给按钮一个onclick事件。我尝试过很多不同的方法,但都没有成功。我做错了什么 下面是我尝试过的 document.getElementById(subDiv).onclick = function() { alert('Error. There is an error on the the page. Please correct that then submit the page'); }; document.getElementById(subDi
document.getElementById(subDiv).onclick = function() { alert('Error. There is an error on the the page. Please correct that then submit the page'); };
document.getElementById(subDiv).onclick = "alert('Error. There is an error on the the page. Please correct that then submit the page');";
function redErrorAlert()
{
alert('Error. There is an error on the the page. Please correct that then submit the page');
}
document.getElementById(subDiv).onclick = redErrorAlert;
document.getElementById(subDiv).setAttribute('onclick',redErrorAlert(), false);
document.getElementById(subDiv).setAttribute('onclick','redErrorAlert()', false);
注意:subDiv是一个包含元素id的变量。在查询DOM树之前,需要等待DOM树的创建 确保所有这些都发生在构建DOM树后创建的上下文中:
window.onload = function() {
document.getElementById(subDiv).onclick = function() { alert('Error. There is an error on the the page. Please correct that then submit the page'); };
};
在对DOM树进行查询之前,需要等待它被创建 确保所有这些都发生在构建DOM树后创建的上下文中:
window.onload = function() {
document.getElementById(subDiv).onclick = function() { alert('Error. There is an error on the the page. Please correct that then submit the page'); };
};
getElementById()接受一个字符串,该字符串包含要查找的元素的ID。假设您正在查找id为'subDiv'的元素,那么应该调用document.getElementById('subDiv')
(代码中的变量subDiv也可能是一个包含ID的字符串,但由于您没有提到它,我假设它没有。)
编辑:如果您同意virstulte关于使用jQuery的建议,您应该在document.ready事件中附加一个函数,以确保在代码运行时已构建DOM。例如:
$(document).ready(function() {
$("#subDiv").click(function() { alert("Test!"); });
});
getElementById()接受一个字符串,该字符串包含要查找的元素的ID。假设您正在查找id为'subDiv'的元素,那么应该调用document.getElementById('subDiv')
(代码中的变量subDiv也可能是一个包含ID的字符串,但由于您没有提到它,我假设它没有。)
编辑:如果您同意virstulte关于使用jQuery的建议,您应该在document.ready事件中附加一个函数,以确保在代码运行时已构建DOM。例如:
$(document).ready(function() {
$("#subDiv").click(function() { alert("Test!"); });
});
这听起来像jQuery领域。一旦您了解了jQuery的来龙去脉,像这样的事情就很容易处理了,您会发现自己编写的JavaScript要少得多 首先,从中获取jQuery 然后将其放入代码中以绑定事件:
$('#idOfElementToBindClickEvent').click(function(){
alert('Error.');
});
jQuery基本上提供了一种使用类似CSS的选择器来操作元素的方法。这听起来像jQuery。一旦您了解了jQuery的来龙去脉,像这样的事情就很容易处理了,您会发现自己编写的JavaScript要少得多 首先,从中获取jQuery 然后将其放入代码中以绑定事件:
$('#idOfElementToBindClickEvent').click(function(){
alert('Error.');
});
jQuery基本上提供了一种使用类似CSS的选择器操纵元素的方法。试试看
document.getElementById(subDiv).onclick = alert('Error. There is an error on the the page. Please correct that then submit the page');
或
第一种情况:您需要调用函数,并且您已经分配了一个字符串
第二种情况:您分配了一个函数,但没有调用此函数
document.getElementById(subDiv).onclick = alert('Error. There is an error on the the page. Please correct that then submit the page');
或
第一种情况:您需要调用函数,并且您已经分配了一个字符串
第二种情况:您已经分配了一个函数,而不是调用这个函数
。您确实应该考虑使用像jQuery这样的JS库,因为这使得这更容易和跨浏览器兼容。也就是说。。你想测试什么浏览器?@ SPIRON,我在Chrome中测试,但是它需要在伊江工作,FF和ChromeYou真的应该考虑使用像JQuery这样的JS库,因为这使得这更容易和跨浏览器兼容。也就是说。。你想在哪个浏览器中测试这个?@spinon我在Chrome中测试,但它需要在IE中工作,FF和ChromesubDiv是一个包含id的变量。很抱歉。啊,就像Jacob说的那样——DOM还没有初始化。我也会考虑Vistulult的优秀的检查jQuery的建议。我已经编辑了我的答案,将Jacob和virstulte的代码结合起来。subDiv是一个包含id的变量。很抱歉。啊,就像Jacob说的——DOM还没有初始化。我也会考虑Vistulult的优秀的检查jQuery的建议。我已经编辑了我的答案,将Jacob和virstulte的代码结合起来。调用时页面已经加载。仅当输入框更改且未验证时,才会调用进行调用的函数。此时应该已经加载DOM树了,不是吗?调用DOM树时页面已经加载了。仅当输入框更改且未验证时,才会调用进行调用的函数。此时应该已经加载了DOM树,不是吗?