我试图创建一个在变更事件时触发的函数,但是没有任何作用。这是我的代码:
var PayContent = document.querySelectorAll('.details');
function OnChange() {
var webForm = document.getElementById('webFormWrap');
webForm.onchange = function(){
PayContent[0].style.display = "none";
button.style.cursor = "default";
document.getElementById('input_field').value = "";
}
// });
}
我尝试在加载时并在设定的时间间隔内调用该函数;也在控制台内。
答案 0 :(得分:1)
不能完全确定您希望在这里完成什么,特别是因为您没有发布任何HTML,但是这应该可以使您接近。
HTML:
<body>
<div class="details" id="hideMe">This element will be hidden</div>
<form id="webFormWrap" onchange="OnChange()">
<input type="text">
</form>
</body>
请注意,我向要隐藏的元素添加了一个ID“ hideMe”。
JavaScript(外部加载)
function OnChange() {
var PayContent = document.querySelectorAll('div.details');
document.getElementById(PayContent[0].id).style.display = "none";
button.style.cursor = "default";
document.getElementById('input_field').value = "";
}
JavaScript不需要在查询选择器中使用“#”。
答案 1 :(得分:1)
您不能只调用函数onchange
并期望它起作用。您必须将事件侦听器添加到元素。 (我假设您将其放在<form id="form">
元素上):
document.getElementById("form").addEventListener("change", OnChange);
这会将change
事件添加到ID为form
的元素。