我的代码格式如下
<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/