如何获取事件的返回值

How to get the return value of a event

本文关键字:事件 返回值 获取 何获取      更新时间:2023-09-26

我使用firebug来查看以下代码,

<input type="text" oncut="return false">

然后尝试获取事件oncut的返回值,

var a=document.querySelector('input[type="text"]');
document.write(a.oncut);

我想得到oncut的返回值:false,然而,我得到了函数oncut的定义,如下所示,

function oncut(event){
return false;
}

如何才能从事件oncut中只得到返回值"false"?

oncut在您的案例中是作为一个处理程序(事件处理程序)。"return false"是该处理程序的定义。要从函数中获取返回值,只需调用函数:

...
document.write(a.oncut());
...

例如:

window.onload = function(){
    document.write(document.getElementsByClassName("cut")[0].oncut());
}
<input type="text" class='cut' oncut="return false">

您可以声明一个函数(给它一个任意的名称),然后将它添加到剪切事件中。将您的陈述放在此函数中。因此,我使用了contentText而不是document.write

function cutFalse() {
  return document.body.textContent = false;
}
<input type="text" oncut="cutFalse()">