[英]Get the selected text on <p> tag
Using jquery or pure javascript, is there a way to get the currently selected text of a <p>
tag? 使用jquery或纯javascript,有没有办法获取
<p>
标签的当前选定文本? I'm aware of input.selectionStart , but it only exists on <input>
. 我知道input.selectionStart ,但它只存在于
<input>
。
Here's a simple solution. 这是一个简单的解决方案。 Works in chrome, safari, FF, and IE9+, but you will have to test on any other legacy browsers you may want to support.
适用于chrome,safari,FF和IE9 +,但您必须在您可能想要支持的任何其他旧版浏览器上进行测试。 http://jsfiddle.net/YEu3k/1/
http://jsfiddle.net/YEu3k/1/
<p id="pText">here is some text</p>
<script>
document.getElementById('pText').onmouseup = function(){
var sel = window.getSelection(), range;
if (sel.getRangeAt) {
range = sel.getRangeAt(0);
alert(range);
}
};
</script>
Try to use https://github.com/madapaja/jquery.selection 尝试使用https://github.com/madapaja/jquery.selection
selection range is different behavior in each browsers. 选择范围是每个浏览器中不同的行为。 this plugin make commonly interfaces to selection range.
这个插件通常与选择范围接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.