gpt4 book ai didi

javascript - 如何找到选择范围所属的父div

转载 作者:行者123 更新时间:2023-11-29 14:53:12 25 4
gpt4 key购买 nike

我的代码格式如下

<div id="viewer">
<div id="pageContainer1">
lot of other divs are inside this
</div>
<div id="pageContainer2">
lot of other divs are inside this
</div>
<div id="pageContainer2">
lot of other divs are inside this
</div>
</div>

我想要的是每当页面上发生文本选择时我想获取它所在的“pageContiner”div。

最佳答案

你是说 this

function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}

$(document).ready(function (){
$('#viewer div *').mouseup(function (e){
alert($(this).closest('[id^="pageContainer"]').attr('id'));
})
});

关于javascript - 如何找到选择范围所属的父div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22010596/

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