[英]HTML / JS - Get selected element class and/or ID
我知道如何使用document.getSelection()
來獲取用戶突出顯示的任何文本的字符串。
但是,如何從getSelection()
訪問class
和/或元素的id
?
有什么想法嗎?
我遇到了你的問題,基本上,getSelection() 方法返回一個選擇 object 代表用戶選擇的文本范圍或插入符號的當前 position,你不能從獲取 ZA2F2ED4F8EBC2CBB4C21A29DCSelectionAB61; 但是,有幾種方法可以從 HTML 頁面獲取 class 和 id 值到 Javascript。
const anything = document.getElementByClassName("Anything")
;const anything = document.getElementById("Anything")
;const selector = document.querySelector(".class #id")
;const cookies = document.getElementById("id-container") cookies.classList.toggle("active")
按照這個策略選擇 classNames 和 id 而不是 getSelection();
在 getSelection() 返回的 object 中,您可以找到focusNode 。 focusNode 包括parentNode 。 在里面你可以找到你正在尋找的 class 和 id。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.