gpt4 book ai didi

javascript - 获取

标签上的选定文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:48:59 27 4
gpt4 key购买 nike

使用 jquery 或纯 javascript,有没有办法获取 <p> 的当前选定文本?标签?我知道 input.selectionStart , 但它只存在于 <input> .

最佳答案

这是一个简单的解决方案。适用于 chrome、safari、FF 和 IE9+,但您必须在您可能想要支持的任何其他旧版浏览器上进行测试。 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>

关于javascript - 获取 <p> 标签上的选定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13597073/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com