Js中的this和window.event.srcElement

我们先看一个简单的例子:

<input type="text" onblur="alert(this.value)"/>完全没有问题。

<wbr></wbr>

那么什么情况下不可以用?

fuction method()

{

<wbr><wbr> alert(this.value);</wbr></wbr>

}

<input type="text" onblur="method()"/>这个就不可以,因为method()被响应函数调用的函数。

<wbr></wbr>

那么这种情况下怎么办?

<wbr></wbr>

方法一:

fuction method(btn)

{

<wbr><wbr> alert(btn.value);</wbr></wbr>

}

<input type="text" onblur="method(this)"/>没问题!

<wbr></wbr>

方法二:

fuction method()

{

<wbr><wbr> alert(window.event.srcElement.value);</wbr></wbr>

}

<input type="text" onblur="method()"/>没问题!window.event.srcElement取得触发事件的控件

<wbr></wbr>

我们在看一个稍微绕一点的例子

<head>
<wbr><wbr><wbr> &lt;script type="text/javascript"&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> function InitEvent() {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> var inputs = document.getElementsByTagName("input");<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> for (var i = 0; i &lt; inputs.length; i++) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong>inputs[i].onblur = OnblurEvent;</strong><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr> function OnblurEvent() {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong><span style="color:#ed1c24">// OnblurEvent是onblur的响应函数,而不是被响应函数调用的函数</span></strong><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="color:#ed1c24"> <strong>// 所以可以用this来获取发生事件的对象</strong></span><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> if (this.value.length &gt; 0) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> this.style.backgroundColor = "white";<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> else {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> this.style.backgroundColor = "red";<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }<br><wbr><wbr><wbr> &lt;/script&gt;<br> &lt;/head&gt;<br> &lt;body onload="InitEvent()"&gt;<br><wbr><wbr><wbr> &lt;input id="Text1" type="text" /&gt;<br><wbr><wbr><wbr> &lt;input id="Text2" type="text" /&gt;<br><wbr><wbr><wbr> &lt;input id="Text3" type="text" /&gt;<br> &lt;/body&gt;<br> &lt;/html&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

正如红色注释解说,上面可以用this获得当前触发事件的input。

你可能感兴趣的:(element)