使用 Rangy 库将类添加到 div



我正在使用 rangy 库将类添加到所选文本中,但我想将其添加到选择中的现有<div>标签中,而不是将类添加到<span>中。

我该如何将其添加到div 中。下面是我用来添加类的代码。

var elem = document.getElementById('frame')
var sel = rangy.getSelection(elem)
var iframeWin = rangy.dom.getIframeWindow(elem);
var boldRedApplier = rangy.createClassApplier("boldRed")
boldRedApplier.applyToSelection(iframeWin)

这里的例子:

var button = document.createElement("input");
button.type = "button";
button.unselectable = true;
button.className = "unselectable"; // This is the line that adds the class

编辑:

要向现有元素添加类,请尝试以下操作:

var elem = document.getElementById('frame');
elem.className = "someclassname";

最新更新