8

我正在尝试在这里找到的示例代码http://api.jquery.com/select/

$(":input").select( function () { 
      $("div").text("Something was selected").show().fadeOut(1000); 
    });

现在我的问题不是打印 "Something was selected" ,而是可以得到准确的选定文本吗?

我想要 jquery .select() 具体答案。我从这里得到了其他解决方案

4

3 回答 3

11

.select()与检索实际选定的文本无关。Select 就像.click():一种将处理程序函数绑定到 select 事件的简单快捷方式。

它在API 文档中说得对:

检索当前选定文本的方法因浏览器而异。许多 jQuery 插件提供跨平台解决方案。

element.select(...)因此,您使用(或者更好的是)绑定element.on("select", ...),然后使用众多跨平台解决方案之一来检索选择。这是基本思想:http: //jsfiddle.net/NjW5a/3/

于 2012-09-21T21:07:45.653 回答
5

.Select() 似乎只是一个在选择该特定元素时触发的事件。但是,要获取文本,您需要使用 javascript 的window.getSelection()函数。

这是一个例子:http: //jsfiddle.net/BCv4Y/3/

    $(":input").select( function () { // on selection, show what's selected
        $("div").text(window.getSelection()).show().fadeOut(1000); 
     }

});

​</p>

也许,如果新版本的 jquery 带有类似$(this).selectedVal()的东西,那就太好了。希望有帮助

于 2012-09-26T21:20:41.683 回答
0
 <script>
window.onmouseup = mouseup;
function mouseup() {
 getSelText();
 }
</script>
   <script  type="text/javascript">
function getSelText() {
var txt = '';
if (window.getSelection) {
    alert (window.getSelection());
} else if (document.getSelection) {
   alert (document.getSelection());
   } else if (document.selection) {
     alert( document.selection.createRange().text);

 } else return;

 }
</script>  
于 2013-08-24T07:05:27.160 回答