简体   繁体   English

我们可以将所有所有信息作为参数传递给内联事件处理程序吗?

[英]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.

相关问题 在内联事件处理程序中引用`this` - Referencing `this` in inline event handlers 如何将参数(事件除外)传递给事件处理程序? - How to pass parameters (other than event) to event handlers? Agility.js + jQueryUI,将参数传递给控制器​​中的事件处理程序 - Agility.js + jQueryUI, pass parameters to event handlers in controller 我可以将元素作为第一个参数传递给 JavaScript 中的事件处理程序的最简单方法是什么? - What's the easiest way i can pass an element as a first argument to event handlers in JavaScript? 我可以使用jQuery删除内联事件处理程序吗? - Can I remove inline event handlers using jQuery? 事件处理程序中的参数-Javascript - parameters in event handlers - Javascript 内联事件处理程序和匿名函数 - Inline event handlers and anonymous functions JavaScript 传参错误,点击事件传中所有参数 - JavaScript pass parameters error, the click event pass all parameters in the pass 将元素作为第一个参数传递给JavaScript中的事件处理程序的最简单方法是什么? - What is the easiest way to pass an element as first argument to event handlers in JavaScript? 如何替换包含参数以符合 CSP 规则的内联事件处理程序(onmousedown、onmouseover、onmouseout...)? - How to replace inline event handlers (onmousedown, onmouseover, onmouseout,…) containing parameters to comply with CSP rules?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM