【发布时间】:2019-05-09 03:39:52
【问题描述】:
这听起来可能很傻,但我在过去 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。
谢谢
【问题讨论】:
-
这似乎是不必要的复杂。获取文本的代码在哪里?如果您正在获取文本,那么获取 div id 应该很简单,
this.id
标签: javascript html