js document.activeElement 获得焦点的元素

<body>
    <input type="text" id="test" value="ajanuw">
    <script>


    document.querySelector('#test').addEventListener('mouseup', (e)=>{
      var activeElement = document.activeElement;
      console.log( activeElement.selectionStart, activeElement.selectionEnd);
      var start = activeElement.selectionStart,
          end = activeElement.selectionEnd;
      console.log( e.target.value.substring(start, end));
    }, false)
    </script>
</body>

posted @ 2017-12-08 16:08  Ajanuw  阅读(366)  评论(0编辑  收藏  举报