gpt4 book ai didi

javascript - 如何从 html 页面中选择某些文本的位置获取 div 的 id

转载 作者:行者123 更新时间:2023-11-28 17:15:08 30 4
gpt4 key购买 nike

这可能听起来很愚蠢,但我在过去的两天里一直在尝试这个事情,

我有一个 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;

关于javascript - 如何从 html 页面中选择某些文本的位置获取 div 的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53668389/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com