相关疑难解决方法(0)

使用原生DOM的最近祖先匹配选择器?

是否有人在DOM api中处理jQuery.closest()等价物?

看起来选择器级别2草案添加matches()等效于jQuery.is(),因此本机最接近应该更容易编写.添加closest()选择器了吗?

javascript jquery standards w3c dom-traversal

15
推荐指数
3
解决办法
1万
查看次数

使用 Window.getSelection() 将所选文本加粗/取消加粗

我正在尝试制作一个简单的文本编辑器,以便用户能够将选定的文本加粗/取消加粗。我想用Window.getSelection()Document.execCommand()。它完全符合我的要求,但是当您将任何文本加粗时,您无法将其取消粗体。我希望它能够加粗和取消加粗任何选定的文本。我尝试了几件事但没有成功。

function addBold(){
const selection = window.getSelection().getRangeAt(0);
const selectedText = selection.extractContents();
const span = document.createElement("span");
span.classList.toggle("bold-span");
span.appendChild(selectedText);
selection.insertNode(span);
};
Run Code Online (Sandbox Code Playgroud)
.bold-span {font-weight: bold;}
Run Code Online (Sandbox Code Playgroud)
<p contentEditable>Bold anything here and unbold it</p>
<button onclick="addBold()">Bold</button>
Run Code Online (Sandbox Code Playgroud)

html javascript css

3
推荐指数
1
解决办法
2061
查看次数

标签 统计

javascript ×2

css ×1

dom-traversal ×1

html ×1

jquery ×1

standards ×1

w3c ×1