gpt4 book ai didi

javascript - 如何用JavaScript模拟点击让当前输入失去焦点

转载 作者:数据小太阳 更新时间:2023-10-29 04:02:15 26 4
gpt4 key购买 nike

我有一个输入,在某些时候恰好有焦点。如果用户单击页面的“背景”,输入将失去焦点。我试图用下面的代码模拟背景上的点击,但这不起作用(你会注意到输入仍然有焦点)。关于如何编写模拟点击页面“背景”的代码的任何建议?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js" ></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/event/event-min.js" ></script>
<script type="text/javascript">
YAHOO.util.Event.onDOMReady(function() {
document.getElementById("input").focus();
document.getElementById("main").focus();
});
</script>
</head>
<body>
<div id="main">
<form action="/">
<p>
<input type="text" id="input"/>
</p>
</form>
</div>
</body>
</html>

最佳答案

我会想象使用 blur() 会达到目的:

<script type="text/javascript">
YAHOO.util.Event.onDOMReady(function() {
document.getElementById("input").focus();
document.getElementById("input").blur();
});
</script>

关于javascript - 如何用JavaScript模拟点击让当前输入失去焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/220488/

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