gpt4 book ai didi

javascript - 如何捕获父元素中的模糊事件

转载 作者:行者123 更新时间:2023-11-28 04:49:54 25 4
gpt4 key购买 nike

我想实现当我从组 1 输入移动到组 2 输入时,模糊事件被捕获,这样我就可以执行其他操作。模糊事件不是向上传播给 parent 吗?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<html>
<head>
<script type="text/javascript">
$( document ).ready(function() {
$("[data-id=container]").on("blur", function() {
alert("Blur caught in parent");
});
});
</script>
</head>
<body>
<div data-id="container" style="border: 1px solid gray;">
Group 1
<input type="text" />
<input type="text" />
<input type="text" />
</div>
<div data-id="container" style="border: 1px solid gray;">
Group 2
<input type="text" />
<input type="text" />
<input type="text" />
</div>
</body>
</html>

最佳答案

我无法完全理解您的问题,但假设您希望在从组 1 更改为组 2 时进行某种形式的验证,我正在编写以下代码。以下代码片段是一个通用表单,您可以根据需要进行自定义。

                <input type="text" id="one" onblur="validate">
<input type="text" id="two" onblur="validate">
<input type="text" id="three" onblur="validate">
<script type="text/javascript>
function validate()
{
var a = document.getElementById("one");
var b = document.getElementById("two");
var c = document.getElementById("three");
if (b.hasFocus() == true) //checks if the second input box has focus
{
alert("Value of the focused field is "+a.value); //this will give you the value in the first input box
}
else if (c.hasFocus() == true) //checks if the third input box has focus
{
a.focus(); //this will get the focus back on the first input box
}
}

</script>

关于javascript - 如何捕获父元素中的模糊事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43005273/

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