如何获取突出显示文本所在的元素?
我正在尝试学习如何编写一个小书签,在其中可以突出显示一些文本,单击小书签并让它告诉我突出显示的内容。我可以做到这一点,但接下来我想知道该文本位于哪个元素中。
例如:
<div id="some-id">to be highlighted</div>
小书签代码:
javascript:(function(){alert(window.getSelection();})()
如果我突出显示“要突出显示”的文本,然后单击小书签,它将提醒该文本。但是我怎样才能获得文本所在的元素,在本例中是之后的元素?
所以流程是:突出显示文本,单击小书签,小书签会告诉您突出显示的内容及其所在的元素。
谢谢!
I am trying to learn how to write a bookmarklet where I can highlight some text, click on the bookmarklet and have it tell me what got highlighted. I can get that far, but next I want to know what element that text is in.
For example:
<div id="some-id">to be highlighted</div>
The bookmarklet code:
javascript:(function(){alert(window.getSelection();})()
If I highlight the text "to be highlighted" and then click on the bookmarklet, it will alert the text. But how can I get the element in which the text is in, in this case the element after that?
So the flow is: highlight text, click bookmarklet, bookmarklet tells you what you highlighted and the element it's in.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试类似的方法来获取包含所选文本的 dom 元素。
它适用于 Firefox 和 Chrome,您应该在其余浏览器中测试它。
它有一个怪癖,如果您选择包含多个元素的文本,则仅返回第一个元素。但也许你可以忍受这个。
仅供参考什么是anchorNode属性:
http://help.dottoro.com/ljkstboe.php
在 Internet Explorer 上,此代码段应该执行以下操作技巧(我无法测试它),
如所述
http://msdn.microsoft.com/en-us/library/ms535872.aspx 和
http://msdn.microsoft.com/en-us/library/ms536654.aspx
关于 quirksmode 的范围说明:http://www.quirksmode.org/dom/range_intro .html
Try something similar to this to get the dom element that contains the selected text.
It works on firefox and Chrome, you should test it into the remaining browsers.
It have a quirk, if you select text that beholds to more than an element, only the first one is returned. But maybe you can live with this.
Just for reference on what is the anchorNode property:
http://help.dottoro.com/ljkstboe.php
On internet explorer this snippet should do the trick (I can't test it)
as stated into
http://msdn.microsoft.com/en-us/library/ms535872.aspx and
http://msdn.microsoft.com/en-us/library/ms536654.aspx
A range explanation on quirksmode: http://www.quirksmode.org/dom/range_intro.html
您可以在所有主要浏览器中相对简单地完成此操作。代码如下,实例: http://jsfiddle.net/timdown/Q9VZT/
You can do this relatively simply in all major browsers. Code is below, live example: http://jsfiddle.net/timdown/Q9VZT/
我认为你不能,他知道当前正在使用哪个元素的唯一方法是在元素上使用 onclick 事件。除此之外没有什么确定的办法。但是,您可以搜索每个元素,直到找到具有相同文本的元素,
您可以添加一个事件来使用此代码获取当前元素(未经测试)
并且它将存储在 selectedElement 中,
好的尝试一下:
DEMO:http://jsfiddle.net/HQC6Z/1/
更好的是: http://jsfiddle.net/HQC6Z/
在查看其他答案后,我收回我的解决方案并提供他们的解决方案:
如何获取突出显示文本所在的元素?
如何获取突出显示文本所在的元素?
I don't think you can, he only way to know which element is currently being used would be to use a onclick event on the element. Other than that there is no sure way. You can however search each element until you find an element with the same text,
You can add an event to get the current element with this code though (untested)
And it will be stored in selectedElement
Ok try This:
DEMO: http://jsfiddle.net/HQC6Z/1/
Better yet: http://jsfiddle.net/HQC6Z/
After looking at the other answers, I take back my solution and offer theirs:
How can I get the element in which highlighted text is in?
How can I get the element in which highlighted text is in?