Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何查找选择范围所属的父div_Javascript_Jquery - Fatal编程技术网

Javascript 如何查找选择范围所属的父div

Javascript 如何查找选择范围所属的父div,javascript,jquery,Javascript,Jquery,我的代码的格式如下 <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"&g

我的代码的格式如下

<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。

您的意思是

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'));
    })
});

提供小提琴让它得到更多的细节。这一个是什么:为什么?这正是问题所在。如果在“pageContainer*”div和n中嵌入的第n级文本选择范围不固定怎么办?@rajesh:你这是什么意思???@Milind我不知道在哪个div下会发生此文本选择。你检查过小提琴吗??