[英]What all information we can pass as parameters to inline event handlers?
I found this code on internet where author is passing id
of the element as first parameter to the event handler. 我在互联网上找到了此代码,作者将元素的id
作为第一个参数传递给事件处理程序。 Refer to the sample below. 请参阅下面的示例。
Until today, I was under the impression that we can pass only event
information as argument to the event handler and I can see that we can pass id
of current element also as parameter to event handler. 直到今天,我的印象是我们只能将event
信息作为参数传递给事件处理程序,并且可以看到我们也可以将当前元素的id
作为参数传递给事件处理程序。
Where is this documented on MDN? 这在MDN上有何记载? Can someone point to me the documentation? 有人可以指向我的文档吗?
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="text" value="Hello" onchange="someFunction(id,event);" id="someId"/>
</body>
<script type="text/javascript">
function someFunction(id,event){
console.log(id); //Printing "someId"
console.log(event.target.value); //It is printing updated value
}
</script>
</html>
I believe you can refer to any property that's somewhere in the element's prototype chain as a standalone variable: 我相信您可以将元素原型链中某处的任何属性称为独立变量:
function someFunction(id, onclick, children, clientTop) { console.log(id); // someId console.log(onclick); // note, this is null, not undefined! console.log(children); // length 0, but still an HTMLCollection console.log(clientTop); // 2 }
<input type="text" value="Hello" onchange="someFunction(id, onclick, children, clientTop);" id="someId" />
It's as if the inline handler is wrapped in a with(this)
. 就像将内联处理程序包装在with(this)
。 Referencing a property name that exists on the element object, or in the element's prototype chain, will result in that property value being referenced. 引用元素对象或元素原型链中存在的属性名称,将导致该属性值被引用。
So, an inline handler like 因此,像
<input onchange="somestr">
is interpreted something like 被解释为
// assume this refers to that input element:
with (this) {
eval(somestr);
}
Note that event
is in a different category - it's not a property of the element, it's a global window.event
. 请注意, event
属于不同的类别-它不是元素的属性,而是全局window.event
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.