简体   繁体   English

如何使用JavaScript获取所选文本

[英]How to get selected text with JavaScript

I'm a little confused why doesn't this code work! 我有点困惑为什么这段代码不起作用!

The HTML Markup: HTML标记:

<div id="diva"><b>Score</b> some <i>goals</i></div>
<div id="soda"></div>

The JavaScript code: JavaScript代码:

function GetSelectedText () {
if (window.getSelection) {  // all browsers, except IE before version 9
    var range = window.getSelection ();
    alert (range.toString ());
} 
else {
    if (document.selection.createRange) { // Internet Explorer
        var range = document.selection.createRange ();
        alert (range.text);
    }
}
}

var butn = document.getElementById("soda");
butn.onclick = function(){
    GetSelectedText();
}

One problem that you may well be experiencing is that in some browsers (notably IE), by the time the button's click event fires, the selection has been destroyed. 您可能遇到的一个问题是,在某些浏览器(特别是IE)中,当按钮的click事件触发时,选择已被破坏。 You can fix this by using the mousedown event instead (which still allows the selection to be destroyed, but only after the event has been handled), or by making the button unselectable . 你可以通过使用mousedown事件来解决这个问题(它仍然允许销毁选择,但仅在事件处理完毕后),或者使按钮不可选

I assume your button is not an actual button input, because this behaviour only happens for regular elements. 我假设你的按钮不是实际的按钮输入,因为这种行为只发生在常规元素上。

Demo: http://jsfiddle.net/L9bvU/1/ 演示: http//jsfiddle.net/L9bvU/1/

 function GetSelectedText () { if (window.getSelection) { // all browsers, except IE before version 9 var range = window.getSelection (); alert (range.toString ()); } else { if (document.selection.createRange) { // Internet Explorer var range = document.selection.createRange (); alert (range.text); } } } 
 span { background-color: #ccc; padding: 3px; border: solid gray 1px; } *[unselectable="on"] { -moz-user-select: -moz-none; -khtml-user-select: none; -webkit-user-select: none; /* Introduced in IE 10. See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/ */ -ms-user-select: none; user-select: none; } 
 <div contenteditable="true">Please select some of this text and press a button below</div> <span onclick="GetSelectedText()">Click</span> <span onmousedown="GetSelectedText()">Mousedown</span> <span unselectable="on" onclick="GetSelectedText()">Click, unselectable</span> 

function getSelectedText() {
    if (window.getSelection) {
        return window.getSelection();
    } 
    if (window.document.getSelection) {
        return window.document.getSelection();
    } 
    if (window.document.selection) {
        return window.document.selection.createRange().text;
    }
    return "";  
}
<p onmouseup="getSelectedText(); alert(txt)">
Highlight some of this text
with the mouse select press button end release
to fire the event. </p>


This is my way . 这是我的方式 You just need to select the text end alert text or anything else. 您只需选择文本结束警报文本或其他任何内容。
Does anyone know how OnMouse(event) set for the entire document in html, and it does not have to be directly added to html page. 有谁知道OnMouse(事件)如何在html中为整个文档设置,并且不必直接添加到html页面。
To be on the other side, as we can change the elements in firebug! 另一方面, 因为我们可以改变萤火虫中的元素! >-examples > -examples

Well there are two problems with the above code. 那么上面的代码有两个问题。 -You can't be guaranteed that - 你无法保证

var butn = document.getElementById("soda");

will work because it may execute before the document is done loading 将起作用,因为它可能在文档加载完成之前执行

-When you click on another element that's not a button, the selection is lost. - 当您单击另一个不是按钮的元素时,选择将丢失。 If you change the "soda" div to then it will work: 如果你将“soda”div改为那么它将起作用:

 <div id="diva"><b>Score</b> some <i>goals</i></div> <div id="soda" onclick="GetSelectedText()">This will NOT work</div> <input type="button" onclick="GetSelectedText()" value="This will work"/> 

However I strongly recommend you look at jQuery as the others here have advised; 但是我强烈建议你看看jQuery,因为这里有其他建议; it will make your like a lot easier! 它会让你喜欢轻松了许多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM