gpt4 book ai didi

javascript - focusin/focusout 和 focus/blur 的区别,举例

转载 作者:IT王子 更新时间:2023-10-29 03:12:27 27 4
gpt4 key购买 nike

谁能通过一个简单的例子告诉我blurfocusoutfocusfocusin 之间的区别?

最佳答案

focusinfocusout 事件冒泡,focusblur 事件不冒泡。这意味着您可以在表单字段的父元素上使用 focusinfocusout

演示:http://jsfiddle.net/pAp4E/

HTML:

<div class="parent">
<input type="text" />
</div>

<div class="log"></div>

Javascript:

$('.parent')
.focusin(function(){log('div focusin');})
.focusout(function(){log('div focusout');})
.focus(function(){log('div focus');})
.blur(function(){log('div blur');});
$('input')
.focusin(function(){log('input focusin');})
.focusout(function(){log('input focusout');})
.focus(function(){log('input focus');})
.blur(function(){log('input blur');});

function log(str){
$('.log').append($('<div/>').text(str));
}

当您运行它时,您会看到只有输入获取所有事件,父级仅获取 focusinfocusout 事件。

关于javascript - focusin/focusout 和 focus/blur 的区别,举例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7858979/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com