簡體   English   中英

如何從html頁面中選擇了某些文本的位置獲取div的ID

[英]how to get id of the div from where some text has been selected in an html page

這聽起來可能很愚蠢,但我在過去2天內一直在講這件事,

我有一個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>

當我從上面的html頁面中選擇一個字符串時,我可以突出顯示它,更改其顏色等,但是我無法從選擇文本的位置獲取div的ID。

代碼以獲取所選文本並將文本加粗

function setAssignment() {

                var range = window.getSelection().getRangeAt(0);

                var selectionContents = range.extractContents();

                var div = document.createElement("span");
                div.id = "assignment";
                div.style.fontWeight = "bold";
                div.style.color ="#FF0000";
                div.appendChild(selectionContents);
                range.insertNode(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的名稱。 請幫我解決這個問題

那就是如果我從div2選擇並突出顯示文本,我需要將其作為div2 ,目前我將其作為mainDiv

謝謝

您的代碼運行正常。

請檢查jsfiddle

http://jsfiddle.net/h96f207e/

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

document.onmouseup = getSelectionParentElement;

這是工作代碼,它將根據選擇給出div1,div 2和div 3。 如果選擇全部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>

您的代碼是好的,但是只有在您想輸入id的名稱時才可以更改return return parent; return parent.id;

 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.id; } document.addEventListener('mouseup',function(){ alert(getSelectionParentElement()); }); 
 <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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM