JavaScript 【事件】焦点事件
<p><span style="font-family: courier new, courier"><strong><strong>以下大部分为学习《JavaScript 高级程序设计》(第 3 版) 所做笔记。</strong></strong></span></p><p><span style="font-family: courier new, courier"><strong><strong>目录:</strong></strong></span></p>
<p><span style="font-family: courier new, courier">1. 了解焦点事件</span></p>
<p><span style="font-family: courier new, courier">2. blur 事件</span></p>
<p><span style="font-family: courier new, courier">3. focus 事件</span></p>
<p> </p>
<p><strong><span style="font-family: courier new, courier; font-size: 18pt"><span style="font-family: courier new, courier">了解焦点事件</span></span></strong></p>
<p><span style="font-family: courier new, courier">Q: 焦点事件什么时候触发?</span></p>
<p><span style="font-family: courier new, courier">A: 当元素获得或失去焦点时触发。</span></p>
<p><span style="font-family: courier new, courier">Q: 焦点事件的用处?</span></p>
<p><span style="font-family: courier new, courier">A: 利用焦点事件并与 document.hasFocus() 方法及 document.activeElement 属性配合,可以知晓用户在页面上的行踪。</span></p>
<p><span style="font-family: courier new, courier">Q: 焦点事件有哪些?</span></p>
<p><span style="font-family: courier new, courier">A: blur DOMFocusIn DomFocusOut focus focusin focusout</span></p>
<p> </p>
<p><span style="font-family: courier new, courier; background-color: rgba(255, 255, 153, 1)"> 浏览器支持 <br></span></p>
<p><span style="font-family: courier new, courier">blur: 所有浏览器都支持</span></p>
<p><span style="font-family: courier new, courier">DOMFocusIn: 只有 Opera 支持</span></p>
<p><span style="font-family: courier new, courier">DomfocusOut: <span style="font-family: courier new, courier">只有 Opera 支持</span></span></p>
<p><span style="font-family: courier new, courier">focus: 所有浏览器都支持</span></p>
<p><span style="font-family: courier new, courier">focusin: 支持的浏览器:IE5.5+、Safari5.1+、Opera11.5+、Chrome</span></p>
<p><span style="font-family: courier new, courier"><span style="font-family: courier new, courier">focusout: <span style="font-family: courier new, courier">支持的浏览器:IE5.5+、Safari5.1+、Opera11.5+、Chrome</span></span></span></p>
<p> </p>
<p><span style="font-family: courier new, courier">IE 的 focusin 和 focusout 与 Opera 的 DOMFocusIn 和 DOMFocusout 发生重叠, IE 的方式最终被 DOM3 级事件采纳为<strong>标准方式</strong>。</span></p>
<p> </p>
<p><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><strong>不冒泡</strong>的事件:blur focus</span></span></span></span></span></p>
<p> ( <span style="font-family: courier new, courier">即使 blur 跟 focus 不冒泡,也可以在捕获阶段侦听它们。)</span></p>
<p> </p>
<p><span style="font-family: courier new, courier"><span style="font-family: courier new, courier">在<strong>失去</strong>焦点的元素上触发, <span style="font-family: courier new, courier"><span style="font-family: courier new, courier">事件目标是失去焦点的元素的事件</span></span>: focusout blur DomFocusOut</span></span></p>
<p><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier">在<strong>获得</strong>焦点的元素上触发, </span></span></span></span></span>事件目标是获得焦点的元素的事件: focusin focus DomfocusIn</span></span></span></span></span></p>
<p> </p>
<p><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier">当焦点从页面的一个元素移动到另一个元素,会依次触发下列事件:focusout focusin blur DOMFocusOut focus DOMFocusIn</span></span></span></span></span></p>
<p> </p>
<p><span style="background-color: rgba(255, 255, 153, 1)"> 检测浏览器是否支持事件 </span>
</p>
<div class="cnblogs_Highlighter">
<pre class="brush:html;gutter:false;">console.log( "onblur" in window ); //输出:true
console.log( "乱打的" in window ); //输出:false
</pre>
</div>
<p> <span style="font-family: courier new, courier"> true: 浏览器支持</span></p>
<p><span style="font-family: courier new, courier">false: 浏览器不支持</span></p>
<p> </p>
<p><strong><span style="font-family: courier new, courier; font-size: 18pt">blur</span></strong></p>
<p><span style="font-family: courier new, courier">下例实现:在文本框中输入英文,文本框时失去焦点之后触发 blur 事件,文本框内容小写变大写</span></p>
<p><span style="font-family: courier new, courier"><strong><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier">方式1:通过 JS 来指定事件处理程序</span></span></span></span></strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:html;collapse:true;;gutter:true;"><!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="EventUtil.js"></script>
<script>
//跨浏览器对象 EventUtil
//为 window 指定 load 事件处理程序
//确保往 DOM 中添加新元素之前页面已经加载完毕,如果在页面加载完毕之前操作 document.body 会出错
EventUtil.addHandler(window, "load", function(event){
var event = EventUtil.getEvent();
//创建一个 input 元素
var input1 = document.createElement("input");
//设置 input1 的 type
input1.type = "text";
//为 input1 指定 blur 事件,元素失去焦点之后触发 blur 事件文本框内文本内容小写变大写
EventUtil.addHandler(input1, "blur", function(){
this.value = this.value.toUpperCase();
});
//添加节点 input1
document.body.appendChild(input1);
});
</script>
</body>
</html>
</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1779827/202006/1779827-20200617215731202-1265225448.png"></p>
<p>(代码中的 EventUtil 对象:https://www.cnblogs.com/xiaoxuStudy/p/13131725.html#three )</p>
<p>页面表现:</p>
<p><img src="https://img2020.cnblogs.com/blog/1779827/202006/1779827-20200617215939319-1643782697.png"></p>
<p>在文本框内输入一个单词</p>
<p><img src="https://img2020.cnblogs.com/blog/1779827/202006/1779827-20200617220124711-156875250.png"></p>
<p>点击框外,文本框失去焦点后,触发了 blur 事件,文本框内容小写变大写</p>
<p><img src="https://img2020.cnblogs.com/blog/1779827/202006/1779827-20200617220231290-656256685.png"></p>
<p><span style="font-family: courier new, courier"><strong><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><strong><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier">方式2:<span style="font-family: courier new, courier">为 <input> 元素添加一个 onblur 特性</span></span></span></span></span></strong></span></span></span></span></strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:html;collapse:true;;gutter:true;"><!DOCTYPE html>
<html>
<body>
<script>
function fn(){
var x = document.getElementById("word");
x.value = x.value.toUpperCase();
}
</script>
<p>输入一个单词:</p>
<input type="text" id="word" onblur="fn()">
</body>
</html>
</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1779827/202006/1779827-20200617203548254-567990253.png"></p>
<p>页面表现同上。</p>
<p> </p>
<p><strong><span style="font-family: courier new, courier; font-size: 18pt">focus<br></span></strong></p>
<p><span style="font-family: courier new, courier">下例实现:文本框获得焦点时触发 focus 事件,修改文本框背景颜色为黄色</span></p>
<p><strong><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier">方式1:通过 JS 来指定事件处理程序</span></span></span></span></strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:html;collapse:true;;gutter:true;"><!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="EventUtil.js"></script>
<script>
//跨浏览器对象 EventUtil
//为 window 指定 load 事件处理程序
//确保往 DOM 中添加新元素之前页面已经加载完毕,如果在页面加载完毕之前操作 document.body 会出错
EventUtil.addHandler(window, "load", function(event){
var event = EventUtil.getEvent();
//创建一个 input 元素
var input1 = document.createElement("input");
//设置 input1 的 type
input1.type = "text";
//为 input1 指定 focus 事件,元素获得焦点之后触发 focus 事件文本框背景颜色变为黄色
EventUtil.addHandler(input1, "focus", function(){
this.style.background = "yellow";
});
//添加节点 input1
document.body.appendChild(input1);
});
</script>
</body>
</html>
</pre>
</div>
<p><span style="font-family: courier new, courier"><img src="https://img2020.cnblogs.com/blog/1779827/202006/1779827-20200617214602218-836908193.png"></span></p>
<p>页面表现:</p>
<p><img src="https://img2020.cnblogs.com/blog/1779827/202006/1779827-20200617213104109-593646029.png"></p>
<p>点击文本框内,input 元素获得焦点后文本框背景颜色变为黄色。</p>
<p><img src="https://img2020.cnblogs.com/blog/1779827/202006/1779827-20200617213210800-955827546.png"></p>
<p><strong><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier"><span style="font-family: courier new, courier">方式2:<span style="font-family: courier new, courier">为 <input> 元素添加一个 onfocus 特性</span></span></span></span></span></strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:html;collapse:true;;gutter:true;"><!DOCTYPE html>
<html>
<body>
<script>
function fn(x){
x.style.background = "yellow";
}
</script>
<input type="text" onfocus="fn(this)">
</body>
</html>
</pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1779827/202006/1779827-20200617214837106-415316462.png"></p>
<p>页面表现同上。</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/xiaoxuStudy/p/13154595.html
頁:
[1]