用execCommand()添加CSS类 [英] Adding CSS Class with execCommand()

查看:224
本文介绍了用execCommand()添加CSS类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function iBlockquote(){
    rich_body_eng.document.execCommand('FormatBlock', false, "<blockquote>");
    var quote = window.getSelection().focusNode.parentNode;
    $(quote).addClass("quote");
}

以上我使用<$ c $为所选文本添加了一个blockquote C>的execCommand()。我想要做的另一件事是向< blockquote添加名为 quote CSS 类标记,所以输出结果将类似于< blockquote class =quote>某些文本< / blockquote> 。它似乎没有工作。请帮助。

Above I'm adding a blockquote to the selected text with execCommand(). Another thing I want to do is add a CSS class called quote to the <blockquote tag, so the output would be something like <blockquote class="quote">Some text</blockquote>. It doesn't seem to work. Please help.

我用这个问题作为指导。 如何添加课程或在execCommand中使用id或CSS样式formatBlock'p'tag?

I'm using this question as a guide. How to add class or id or CSS style in execCommand formatBlock 'p' tag?

推荐答案

无法在Firefox中重现,这里是一个 JSFiddle ,我试着用jQuery和普通的JavaScript添加类,两者都有效。

HTML



Cannot reproduce in Firefox, here is a JSFiddle, I've tried adding the class with jQuery and with plain JavaScript, both work.

<div contentEditable="true"></div>



CSS



CSS


这只是将 blockquote



.quote{
    width:300px;
    height:100px;
    background-color:khaki;
}



JavaScript



JavaScript

document.designMode = "on";
document.execCommand('formatBlock', false, "<blockquote>");
var quote = window.getSelection().focusNode.parentNode;
$(quote).addClass("quote");
//quote.className = "quote";

也许这有帮助,也许我做了不同的事情:)

Maybe this helps, maybe I did something differently :)

这篇关于用execCommand()添加CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆