Javascript 如何从html页面中选择文本的位置获取div的id

Javascript 如何从html页面中选择文本的位置获取div的id,javascript,html,Javascript,Html,这听起来可能很傻,但我在过去的两天里一直在尝试这件事 我有一个html页面。请查看代码以获得想法 <div id="mainDiv"> <div id = "div1"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 15

这听起来可能很傻,但我在过去的两天里一直在尝试这件事

我有一个html页面。请查看代码以获得想法

<div id="mainDiv">
  <div id = "div1">
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
  <div id = "div2">
  It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
  </div>
  <div id = "div3">
  Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
  </div>
</div>
在搜索了很多之后,我得到了这个代码

function getSelectionParentElement() {
  var parent = null,
  selection;
  if (window.getSelection) {
    selection = window.getSelection();
    if (selection.rangeCount) {
      parent = selection.getRangeAt(0).commonAncestorContainer;
      if (parent.nodeType != 1) {
          parent = parent.parentNode;
      }
    }
  } else if ((selection = document.selection) && selection.type != "Control") {
    parent = selection.createRange().parentElement();
  }
  return parent;
}
但此函数返回
mainDiv
。它没有给我从中选择文本的div的名称。 请帮我解决这个问题

也就是说,如果我从say
div2
中选择并突出显示文本,我需要它作为
div2
,当前我作为
mainDiv
获取它


谢谢

您的代码运行良好

请检查JSFIDLE


你的代码运行良好

请检查JSFIDLE


这是工作代码,它将根据选择给出div1、div2和div3。如果您选择全部3,那么它将为您提供mainDiv

<!DOCTYPE html>
<html>
<body>
<div id="mainDiv" onMouseUp="getSelectionParentElement()">
  <div id = div1>
  Div 1
  </div>
  <div id = div2>
  Div 2
  </div>
  <div id = div3>
 Div 3
 </div>
</div>
<script>
function getSelectionParentElement() {
  var parent = null,
  selection;
  if (window.getSelection) {
    selection = window.getSelection();
    if (selection.rangeCount) {
      parent = selection.getRangeAt(0).commonAncestorContainer;
      if (parent.nodeType != 1) {
          parent = parent.parentNode;
      }
    }
  } else if ((selection = document.selection) && selection.type != "Control") {
    parent = selection.createRange().parentElement();
  }
  alert(parent.id);
}
</script>

</body>
</html>

第一组
第2组
第3组
函数getSelectionParentElement(){
var parent=null,
选择;
if(window.getSelection){
selection=window.getSelection();
if(selection.rangeCount){
parent=selection.getRangeAt(0).commonAncestorContainer;
if(parent.nodeType!=1){
parent=parent.parentNode;
}
}
}else if((selection=document.selection)&&selection.type!=“Control”){
parent=selection.createRange().parentElement();
}
警报(parent.id);
}

这是工作代码,它将根据选择给出div1、div2和div3。如果您选择全部3,那么它将为您提供mainDiv

<!DOCTYPE html>
<html>
<body>
<div id="mainDiv" onMouseUp="getSelectionParentElement()">
  <div id = div1>
  Div 1
  </div>
  <div id = div2>
  Div 2
  </div>
  <div id = div3>
 Div 3
 </div>
</div>
<script>
function getSelectionParentElement() {
  var parent = null,
  selection;
  if (window.getSelection) {
    selection = window.getSelection();
    if (selection.rangeCount) {
      parent = selection.getRangeAt(0).commonAncestorContainer;
      if (parent.nodeType != 1) {
          parent = parent.parentNode;
      }
    }
  } else if ((selection = document.selection) && selection.type != "Control") {
    parent = selection.createRange().parentElement();
  }
  alert(parent.id);
}
</script>

</body>
</html>

第一组
第2组
第3组
函数getSelectionParentElement(){
var parent=null,
选择;
if(window.getSelection){
selection=window.getSelection();
if(selection.rangeCount){
parent=selection.getRangeAt(0).commonAncestorContainer;
if(parent.nodeType!=1){
parent=parent.parentNode;
}
}
}else if((selection=document.selection)&&selection.type!=“Control”){
parent=selection.createRange().parentElement();
}
警报(parent.id);
}

您的代码很好,但只有当您想返回id的名称时才会更改
返回父项
返回parent.id

函数getSelectionParentElement(){ var parent=null, 选择; if(window.getSelection){ selection=window.getSelection(); if(selection.rangeCount){ parent=selection.getRangeAt(0).commonAncestorContainer; if(parent.nodeType!=1){ parent=parent.parentNode; } } }else if((selection=document.selection)&&selection.type!=“Control”){ parent=selection.createRange().parentElement(); } 返回parent.id; } document.addEventListener('mouseup',function(){ 警报(getSelectionParentElement()); });

Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布而流行,最近随着Aldus PageMaker等桌面出版软件的发布,包括Lorem Ipsum版本。
读者在查看页面布局时会被页面的可读内容分散注意力,这是一个早已确定的事实。使用Lorem Ipsum的意义在于,它的字母分布或多或少是正态的,而不是使用“此处内容,此处内容”,使其看起来像可读的英语。许多桌面发布软件包和网页编辑器现在使用Lorem Ipsum作为默认模型文本,搜索“Lorem Ipsum”将发现许多尚处于起步阶段的网站。多年来,各种版本不断演变,有时出于偶然,有时出于故意(注入幽默等)。
与流行的观点相反,Lorem Ipsum不是简单的随机文本。它起源于公元前45年的一段古典拉丁文学,距今已有2000多年的历史。弗吉尼亚州汉普顿悉尼学院的拉丁语教授理查德·麦克林托克(Richard McClintock)从《洛伦·伊普斯姆》(Lorem Ipsum)一段中查找了一个更为晦涩的拉丁语单词,即“Concertetur”,并查阅了古典文学中对该词的引用,发现了该词无可置疑的来源。Lorem Ipsum来自西塞罗于公元前45年所著《德菲尼布斯·博诺勒姆和马洛勒姆》(善与恶的极端)的第1.10.32节和第1.10.33节。这本书是一本关于伦理学理论的论文,在文艺复兴时期非常流行。Lorem Ipsum的第一行“Lorem Ipsum dolor sit amet..”来自第1.10.32节中的一行。

您的代码很好,但只有当您想返回id的名称时才会更改
返回父项
返回parent.id

函数getSelectionParentElement(){ var parent=null, 选择; if(window.getSelection){ selection=window.getSelection(); if(selection.rangeCount){ parent=selection.getRangeAt(0).commonAncestorContainer; if(parent.nodeType!=1){ parent=parent.parentNode; } } }else if((selection=document.selection)&&selection.type!=“Control”){ parent=selection.createRange().parentElement(); } 返回parent.id; } document.addEventListener('mouseup',function(){ 警报(getSelectionParentElement()); });

Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。它在20世纪60年代随着Letraset的发行而流行起来
<!DOCTYPE html>
<html>
<body>
<div id="mainDiv" onMouseUp="getSelectionParentElement()">
  <div id = div1>
  Div 1
  </div>
  <div id = div2>
  Div 2
  </div>
  <div id = div3>
 Div 3
 </div>
</div>
<script>
function getSelectionParentElement() {
  var parent = null,
  selection;
  if (window.getSelection) {
    selection = window.getSelection();
    if (selection.rangeCount) {
      parent = selection.getRangeAt(0).commonAncestorContainer;
      if (parent.nodeType != 1) {
          parent = parent.parentNode;
      }
    }
  } else if ((selection = document.selection) && selection.type != "Control") {
    parent = selection.createRange().parentElement();
  }
  alert(parent.id);
}
</script>

</body>
</html>