Javascript 只有内联处理程序才能工作
在我的应用程序上,只有内联处理程序有效 这是我的Javascript函数的一部分Javascript 只有内联处理程序才能工作,javascript,html,cordova,dom-events,Javascript,Html,Cordova,Dom Events,在我的应用程序上,只有内联处理程序有效 这是我的Javascript函数的一部分 function onDeviceReady() { alert('ready'); document.querySelector('#idCpf').onkeypress = alert('keypress'); document.getElementById("idCpf").onclick=alert("click");
function onDeviceReady() {
alert('ready');
document.querySelector('#idCpf').onkeypress = alert('keypress');
document.getElementById("idCpf").onclick=alert("click");
}
这是我的html代码的一部分
<input type="tel" autocomplete="on" id="idCpf" onblur="alert('onblur')" placeholder="Seu CPF" required>
在ondevicerady
功能上,只有第一个警报起作用,并且onkeypress
和onclick
当功能ondevicerady
未按下时,单击或模糊输入,向我显示警报。但是我的代码只有在使用内联处理程序时才有效,比如在input-type。。。。onblur=“警报('onblur')”
我有什么问题
document.querySelector('#idCpf').onkeypress = alert('keypress');
调用警报(“按键”)代码>并指定警报('keypress')的返回值代码>作为事件处理程序
警报(“按键”)代码>立即执行,而不是在触发事件时执行
要分配事件处理程序,必须执行以下操作:
document.querySelector('#idCpf').onkeypress = function () { alert('keypress') };
document.getElementById("idCpf").onclick= function () { alert("click") };
您正在调用alert
并将alert
的返回值分配给属性,而不是函数。您必须为这些属性分配一个函数。可能的重复项