JQ的select事件,怎么做到只輸出選擇的文本部分,代碼具體代碼是啥
JQ的select事件,怎么做到只輸出選擇的文本部分,代碼具體代碼是啥
<body>
? ? <h2>input與textarea</h2>
? ? <div class="left">
? ? ? ? <h4>測(cè)試一</h4>
? ? ? ? <div class="aaron">
? ? ? ? ? ? 選中文字:input
? ? ? ? ? ? <input type="text" value="慕課網(wǎng)" />
? ? ? ? </div>
? ? ? ? <button id="bt1">觸發(fā)input元素的select事件</button>
? ? ? ??
? ? ? ? <h4>測(cè)試二</h4>
? ? ? ? <div class="aaron">
? ? ? ? ? ? textarea:
? ? ? ? ? ? <textarea rows="3" cols="20">用鼠標(biāo)選中文字</textarea>
? ? ? ? </div>
? ? </div>
?
? ? <script type="text/javascript">
? ? //監(jiān)聽(tīng)input元素中value的選中
? ? //觸發(fā)元素的select事件
? ? $("input").select(function(e){
? ? ? ? alert(e.target.value)
? ? })
? ? $("#bt1").click(function(){
? ? ? ? $("input").select();
? ? })
? ? //監(jiān)聽(tīng)textarea元素中value的選中
? ? $('textarea').select(function(e) {
? ? ? ? alert(e.target.textContent);
? ? });
? ? </script>
</body>
</html>
e.target.value應(yīng)該怎么改?
2017-03-02
我試了一下這樣可以:
alert(e.target.value.substring(e.currentTarget.selectionStart,e.currentTarget.selectionEnd));
不知道有沒(méi)有更好的方法