【问题标题】:Onselect event doesn't fire on divOnselect 事件不会在 div 上触发
【发布时间】:2010-09-28 14:31:18
【问题描述】:

我无法触发附加到

元素的 onselect 事件处理程序。是否可以强制
发出 select 事件?

【问题讨论】:

    标签: html onselect


    【解决方案1】:

    根据w3schools,onSelect 只定义在 input 和 textarea 对象上。

    您可以尝试在当前窗口/文档中捕捉MouseUp eventretrieve the selected text

    【讨论】:

      【解决方案2】:

      您可以通过使用onMouseUp 事件来实现您想要的。见下文...

      <html>
          <body>
              <div id="container" style="border: 1px solid #333" contentEditable="true" onMouseUp="checkMe()">Type text    here</div>
      
          </body>
          <script language="javascript">
          function checkMe() {
              var txt = "";
      
              if (window.getSelection) {
                      txt = window.getSelection();
              }
              else if (document.getSelection) {
                  txt = document.getSelection();
              } else if (document.selection) {
                  txt = document.selection.createRange().text;
              }
      
              alert("Selected text is " + txt);
          }
          </script>
      </html>
      

      【讨论】:

      • 这或多或少是我正在寻找的,但它没有考虑用户使用键盘扩展或移动选择的可能性。但是,将相同的逻辑绑定到 onKeyUp 可能会起作用。
      【解决方案3】:

      使用OnClick

      :select 仅适用于某些表单元素。

      【讨论】:

        猜你喜欢
        • 2019-10-10
        • 2011-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多