点击 div 后返回输入框焦点

3
为了方便在输入框中输入带重音的字符,我展示一个弹出层,其中显示这些字符,以便您可以选择并点击。我想保持输入框的焦点(或在任何将其移开的操作后返回)。单击以输入重音后,焦点将不会在JavaScript控制下返回。我已使用Mac Safari和Firefox进行测试,但无法正常工作。最终它需要在所有平台上运行。任何帮助都将不胜感激。David
    <!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 onload="document.getElementById('inputbox').focus();">
    <div id="accentswindow" >
      <div class="accentlink" onmousedown="insertchar('À')">À</div>
      <div class="accentlink" onmousedown="insertchar('Û')">Û</div>
      <img src="http://www.revilolang.com/images/closebox.png" width="28" height="26" style="float:right;" onmouseup="displayaccents()"/> </div>
    <input name="" id="inputbox" type="text" />
    <input name="accents" type="button" value="accents"onclick="displayaccents()" />
    </body>
    </html>
3个回答

3

由于鼠标弹起后焦点仍然存在,因此您需要添加一个监听器来设置焦点。

<!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 onload="addListeners();document.getElementById('inputbox').focus();">
    <div id="accentswindow" >
      <div class="accentlink" onmousedown="insertchar('À')">À</div>
      <div class="accentlink" onmousedown="insertchar('Û')">Û</div>
      <img src="http://www.revilolang.com/images/closebox.png" width="28" height="26" style="float:right;" onmouseup="displayaccents()"/> </div>
    <input name="" id="inputbox" type="text" />
    <input name="accents" type="button" value="accents"onclick="displayaccents()" />
    </body>
    </html>

我自己解决了这个问题 - 我需要在整个过程中使用mouseup - 我之前只有一个mousedown和两个mouseups! - David Eno

1

好的,您需要将焦点绑定到失焦(模糊)操作。

如果您有jQuery,可以这样做:

  inputbox.blur(function(){
       inputbox.focus();
  });

纯JavaScript的代码可能是这样的:

  inputbox.onblur=function(){
       inputbox.focus();
  };

谢谢你的回复,但我想我已经回答了自己的问题 - 我需要在整个过程中使用mouseup! - David Eno

1

请在每个 accentlink DIV 上添加 onclick = "document.getElementById('inputbox').focus();"

例如:

<div class="accentlink" onclick = "javascript:document.getElementById('inputbox').focus();" onmousedown="javascript:insertchar('À');">À</div>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接