Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中单击元素时获取选定文本_Javascript - Fatal编程技术网

如何在javascript中单击元素时获取选定文本

如何在javascript中单击元素时获取选定文本,javascript,Javascript,我有一个具有[contenteditable=true]属性的div。当我选择一些文本并单击一个按钮时,我想获取所选文本。我做了以下工作: $(".btn").click(function (){ alert(window.getSelection()); }); 上述代码不会发出任何警报。如果我将.btn元素更改为img元素,警报将输出所选文本。单击任何元素时,我可以做什么来获取选定的文本 这是我的html代码: <li class="btn">Selection <

我有一个具有
[contenteditable=true]
属性的div。当我选择一些文本并单击一个按钮时,我想获取所选文本。我做了以下工作:

$(".btn").click(function (){
   alert(window.getSelection());
});
上述代码不会发出任何警报。如果我将
.btn
元素更改为
img
元素,警报将输出所选文本。单击任何元素时,我可以做什么来获取选定的文本

这是我的html代码:

<li class="btn">Selection </li>
<div contenteditable="true">Some text here</div>
选择 这里有一些文字

函数getSelText(){
var-txt='';
if(window.getSelection){
txt=window.getSelection();
}
else if(document.getselectiOn){
txt=document.getSelection();
}
else if(文档选择){
txt=document.selection.createRange().text;
}
否则返回;
警报(txt)
}
window.document.onmousedown=函数(){
getSelText()
}

函数getSelText(){
var-txt='';
if(window.getSelection){
txt=window.getSelection();
}
else if(document.getselectiOn){
txt=document.getSelection();
}
else if(文档选择){
txt=document.selection.createRange().text;
}
否则返回;
警报(txt)
}
window.document.onmousedown=函数(){
getSelText()
}

函数getSelText(){
var-txt='';
if(window.getSelection){
txt=window.getSelection();
}
else if(document.getselectiOn){
txt=document.getSelection();
}
else if(文档选择){
txt=document.selection.createRange().text;
}
否则返回;
警报(txt)
}
window.document.onmousedown=函数(){
getSelText()
}

函数getSelText(){
var-txt='';
if(window.getSelection){
txt=window.getSelection();
}
else if(document.getselectiOn){
txt=document.getSelection();
}
else if(文档选择){
txt=document.selection.createRange().text;
}
否则返回;
警报(txt)
}
window.document.onmousedown=函数(){
getSelText()
}

当您选择所需文本并单击按钮时,正如Ramon&adeneo在其评论中解释的那样,焦点将丢失。按钮命令在
mouseup
处执行,此时选择被清除。据我所知,您的代码使用图像的原因是,与按钮不同,图像命令是在
mousedown
处执行的


我的建议是尝试使用
.on('mousedown',…)
命令,而不是简单的单击命令。

当您选择所需文本并单击按钮时,正如Ramon&adeneo在其评论中解释的那样,焦点会丢失。按钮命令在
mouseup
处执行,此时选择被清除。据我所知,您的代码使用图像的原因是,与按钮不同,图像命令是在
mousedown
处执行的


我的建议是尝试使用
.on('mousedown',…)
命令,而不是简单的单击命令。

当您选择所需文本并单击按钮时,正如Ramon&adeneo在其评论中解释的那样,焦点会丢失。按钮命令在
mouseup
处执行,此时选择被清除。据我所知,您的代码使用图像的原因是,与按钮不同,图像命令是在
mousedown
处执行的


我的建议是尝试使用
.on('mousedown',…)
命令,而不是简单的单击命令。

当您选择所需文本并单击按钮时,正如Ramon&adeneo在其评论中解释的那样,焦点会丢失。按钮命令在
mouseup
处执行,此时选择被清除。据我所知,您的代码使用图像的原因是,与按钮不同,图像命令是在
mousedown
处执行的


我的建议是尝试使用
.on('mousedown',…)
命令,而不是简单的单击命令。

可能是焦点问题。按钮获得焦点,选择内容丢失。您是否也可以共享您的HTMLM确保选择内容不会失去焦点,然后尝试
toString()
?没有错误。除了img之外的任何元素,所选文本都不会显示。在这里工作正常。您可以分享您的html吗?如果有您的CSP,这可能是一个焦点问题。按钮获得焦点,选择内容丢失。您是否也可以共享您的HTMLM确保选择内容不会失去焦点,然后尝试
toString()
?没有错误。除了img之外的任何元素,所选文本都不会显示。在这里工作正常。您可以分享您的html吗?如果有您的CSP,这可能是一个焦点问题。按钮获得焦点,选择内容丢失。您是否也可以共享您的HTMLM确保选择内容不会失去焦点,然后尝试
toString()
?没有错误。除了img之外的任何元素,所选文本都不会显示。在这里工作正常。您可以分享您的html吗?如果有您的CSP,这可能是一个焦点问题。按钮获得焦点,选择内容丢失。您是否也可以共享您的HTMLM确保选择内容不会失去焦点,然后尝试
toString()
?没有错误。除了img之外的任何元素,所选文本都不会显示。在这里工作很好。你能分享你的html吗?如果有你的css
    <script language=javascript>
    function getSelText()  {
        var txt = '';
        if (window.getSelection) {
            txt = window.getSelection();
        }
        else if (document.getSelectiion) {
            txt = document.getSelection();
        }
        else if (document.selection) {
           txt = document.selection.createRange().text;
        }
        else return;
        alert(txt)
     }


      window.document.onmousedown = function() {
        getSelText()
     }
     </script>