admin管理员组文章数量:1334395
window.getSelection().anchorNode
returns quite a lot of details about the node where the user clicked to start the selection, but how can I get attributes of that text node, like class
, id
etc.?
Example:
<span id="word1">Aaa</span>
<span id="word2">Bbb</span>
The user selects something of these two spans and I need to know where he started the selection, whether in #word1
or in #word2
window.getSelection().anchorNode
returns quite a lot of details about the node where the user clicked to start the selection, but how can I get attributes of that text node, like class
, id
etc.?
Example:
<span id="word1">Aaa</span>
<span id="word2">Bbb</span>
The user selects something of these two spans and I need to know where he started the selection, whether in #word1
or in #word2
-
1
text node
hasclass
andid
? – xianshenglu Commented Apr 8, 2018 at 9:36 -
A text
node
can't have aclass
orid
– Asons Commented Apr 8, 2018 at 9:38 -
@xianshenglu I think so, if it is something like
<span id="myText" class="grey-text">Something</span>
... – Örom Loidl Commented Apr 8, 2018 at 9:38 -
No, even in this case, the TextNode
Something
will just be a child of the Span#myText Element. – Kaiido Commented Apr 8, 2018 at 11:04
3 Answers
Reset to default 6guess you need this:window.getSelection().anchorNode.parentNode
window.onclick = function() {
console.log(window.getSelection().anchorNode.parentNode)
console.log(window.getSelection().anchorNode.parentNode.className);
console.log(window.getSelection().anchorNode.parentNode.id)
}
<p class="cls" id="p1">p tag with class="cls" and id="p1",try to select something</p>
Since textNodes do not have any attributes you will have to get the attributes from the element parent. The select event
has spotty support so I used the mousedown event
and registered the Document Object to listen for it. In order to control where and when you get values from among 100 possibilities (remember the Document Object will be aware of mousedown
event on anything but the Window Object), we must establish 2 things:
Event.currentTarget: A property of the Event Object, that represents the element registered to the event. In the Demo
e.currentTarget
is the Document Object (document
.)Event.target: A property of the Event Object that represents the origin of an event, which is fancy talk for the element that was clicked, changed, hovered over, etc. In the Demo
e.target
is basically anything in thedocument
.
The following Demo demonstrates a way to get the id and/or classes of a clicked element node.
Demo
Details are mented in Demo
document.addEventListener('mousedown', showAttr, false);
function showAttr(e) {
// if the node clicked is NOT document...
if (e.target !== e.currentTarget) {
/* if the clicked node has a class attribute...
|| log all of its classes in console
*/
var tgt = e.target;
if (tgt.hasAttribute('class')) {
var cList = tgt.classList;
console.log('class=' + cList);
}
/* if the clicked node has an id...
|| log its id in console
*/
if (tgt.hasAttribute('id')) {
console.log('id=' + tgt.id);
}
}
return false;
}
.as-console-wrapper {
width: 30%;
margin-left: 70%;
min-height: 100%;
}
main,
main * {
outline: 1px solid black
}
<main id='base'>
<h1 class='mainHeading'>Title</h1>
<ol class='ordered list'>
<li class='1'>One</li>
<li class='2'>Two</li>
<li class='3'>Three</li>
</ol>
Text
<article id='post31' class='test'>
<h2 class='postHeading'>Title</h2>
<p class='content text'>Post content</p>
article text
</article>
</main>
</main>
This question has been asked a while ago but I found a way to do this, you can access the attribute by doing the following:
window.getSelection().anchorNode.parentElement.getAttribute('your-attribute-name')
This will get the DOM element that surrounds the selected text and with access to the DOM element you can retrieve it's attributes.
本文标签: javascriptGet Attributes of windowgetSelection()anchorNodeStack Overflow
版权声明:本文标题:javascript - Get Attributes of window.getSelection().anchorNode - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742237564a2438358.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论