javascript – 单击div后将焦点返回到输入字段
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 单击div后将焦点返回到输入字段,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4102字,纯文字阅读大概需要6分钟。
内容图文
![javascript – 单击div后将焦点返回到输入字段](/upload/InfoBanner/zyjiaocheng/722/e5d37fc72b8a4d76828775a60ce9f779.jpg)
为了便于在输入字段中输入重音字符,我会显示一个弹出div,显示字符,以便您选择并单击.我希望将注意力集中在输入字段上(或在任何将其取走的操作之后将其返回).单击以输入重音后,焦点将不会在javascript控制下返回.我已经使用Mac Safari和Firefox进行了测试,但没有去过.最终它需要在所有平台上运行.
任何帮助非常感谢.
大卫
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" charset="utf-8">
var showaccentswindow = 0;
function displayaccents() {
if (showaccentswindow==0) {
document.getElementById('accentswindow').style.display='block';
showaccentswindow=1;
}
else
{
document.getElementById('accentswindow').style.display='none';
showaccentswindow=0;
}
document.getElementById('inputbox').focus();
}
function insertchar(c) {
/*for inserting accented chars*/
document.getElementById('inputbox').value=document.getElementById('inputbox').value + c;
document.getElementById('inputbox').focus(); **// DOESN'T WORK!!!**
}
</script>
<style type="text/css">
#accentswindow {
display:none;
position:absolute;
top: 50px;
background-color: #FFF;
width: 200px;
border: 2px solid #999;
z-index: 100;
left: 150px;
text-align: left;
padding-bottom: 20px;
padding-left: 20px;
}
.accentlink {
background-color: #F3EED7;
text-align: center;
display: block;
float: left;
height: 22px;
width: 17px;
margin-top: 9px;
margin-right: 3px;
font-family: Verdana, Geneva, sans-serif;
font-size: 15px;
font-weight: bold;
color: #666;
cursor: pointer;
}
</style>
</head>
<body onl oad="document.getElementById('inputbox').focus();">
<div id="accentswindow" >
<div class="accentlink" onm ousedown="insertchar('à')">à</div>
<div class="accentlink" onm ousedown="insertchar('?')">?</div>
<img src="http://www.revilolang.com/images/closebox.png" width="28" height="26" style="float:right;" onm ouseup="displayaccents()"/> </div>
<input name="" id="inputbox" type="text" />
<input name="accents" type="button" value="accents"onclick="displayaccents()" />
</body>
</html>
解决方法:
由于mouseup“仍然”成为焦点,你应该添加一个监听器来设置焦点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" charset="utf-8">
var showaccentswindow = 0;
function displayaccents() {
if (showaccentswindow==0) {
document.getElementById('accentswindow').style.display='block';
showaccentswindow=1;
}
else
{
document.getElementById('accentswindow').style.display='none';
showaccentswindow=0;
}
document.getElementById('inputbox').focus();
}
function insertchar(c) {
/*for inserting accented chars*/
document.getElementById('inputbox').value=document.getElementById('inputbox').value + c;
// DOESN'T WORK!!!**
}
function addListeners(){
document.getElementById('accentswindow').addEventListener("mouseup",function(){
document.getElementById('inputbox').focus();
})
}
</script>
<style type="text/css">
#accentswindow {
display:none;
position:absolute;
top: 50px;
background-color: #FFF;
width: 200px;
border: 2px solid #999;
z-index: 100;
left: 150px;
text-align: left;
padding-bottom: 20px;
padding-left: 20px;
}
.accentlink {
background-color: #F3EED7;
text-align: center;
display: block;
float: left;
height: 22px;
width: 17px;
margin-top: 9px;
margin-right: 3px;
font-family: Verdana, Geneva, sans-serif;
font-size: 15px;
font-weight: bold;
color: #666;
cursor: pointer;
}
</style>
</head>
<body onl oad="addListeners();document.getElementById('inputbox').focus();">
<div id="accentswindow" >
<div class="accentlink" onm ousedown="insertchar('à')">à</div>
<div class="accentlink" onm ousedown="insertchar('?')">?</div>
<img src="http://www.revilolang.com/images/closebox.png" width="28" height="26" style="float:right;" onm ouseup="displayaccents()"/> </div>
<input name="" id="inputbox" type="text" />
<input name="accents" type="button" value="accents"onclick="displayaccents()" />
</body>
</html>
内容总结
以上是互联网集市为您收集整理的javascript – 单击div后将焦点返回到输入字段全部内容,希望文章能够帮你解决javascript – 单击div后将焦点返回到输入字段所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。