我希望在自己的富文本编辑器中使用execcommand 'formatblock'命令,以选择由'p'标签、特定类或Id、任何css样式定义的行。我已经搜索了很多资料,但都没有找到对我有价值的内容。
document.execCommand('formatblock', false, 'p');
我该如何在这段代码中添加类(class)、id或CSS样式?
请问您需要添加的具体内容是什么?
我希望在自己的富文本编辑器中使用execcommand 'formatblock'命令,以选择由'p'标签、特定类或Id、任何css样式定义的行。我已经搜索了很多资料,但都没有找到对我有价值的内容。
document.execCommand('formatblock', false, 'p');
如果您想为可编辑的 div 添加 CSS 的 id 或 class,则可以使用以下代码 ---
<script>
function CssFnctn()
{
document.execCommand('formatblock', false, 'p')
var listId = window.getSelection().focusNode.parentNode;
$(listId).addClass("oder2");
}
</script>
.oder2
{
padding-left: 3em;
}
有很多方法可以实现它。只需使用 execCommand 'insertHTML',将选定的文本替换为包装的代码即可。像这样:
selection = window.getSelection().toString();
wrappedselection = '<span class="accent" style="somestyle">' + selection + '</span>';
document.execCommand('insertHTML', false, wrappedselection);
这将会移除掉换行符,像<b>
、<i>
和其他的内部HTML格式标签——如果你想要保留它们,你需要像这样做(感谢这篇文章):
selection = window.getSelection().getRangeAt(0).cloneContents();
span = document.createElement('span');
span.appendChild(selection);
wrappedselection = '<span class="accent1">'+span.innerHTML+'</span>';
document.execCommand('insertHTML', false, wrappedselection);
这个insertHTML在IE上不起作用。请查阅文档https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
I got the solution.
Javascript:
function line(){
window.execCommand('formatblock', false, 'p');
selectedElement = window.getSelection().focusNode.parentNode;
selectedElement.style.marginBottom = '100px';
}
HTML
<input type="button" value="addMarginBottom" onclick="javascript:line();"/>
<div class="textcontent" contenteditable ="true"></div>
这对我来说完全可以工作。但是我现在无法制作jsfiddle。这对单行正常工作,但不适用于多行。
请尝试以下代码:http://jsfiddle.net/lotusgodkk/GCu2D/57/
Javascript:
$(document).ready(function () {
$('div').click(function () {
var sel = window.getSelection();
var range = document.createRange();
el = document.getElementById('one'); //Get the element
range.selectNodeContents(el);
sel.removeAllRanges();
sel.addRange(range);
document.execCommand('formatblock', false, null); //execute command.
document.execCommand('bold', false, null); //execute command.
});
});
HTML
<div contenteditable="true">
<p id="one">Sample text</p>
<p>Sample text 2</p>
</div>
function CssFnctn() {
document.execCommand('formatblock', false, 'p')
var listId = window.getSelection().anchorNode.parentNode;
listId.classList = 'oder2';
}
我曾经遇到过同样的问题,最终我使用了jQuery。
document.execCommand(optionCommand, false, null);
let snippets = $('.js-editor-content-snippet');
let lists = snippets.find('ul, ol');
lists.css({ margin: '0', padding: '0 0 0 20px' });
lists.find('li').css({ margin: '0 0 12px' });
还有一个非常棒的库可能会很有帮助:https://github.com/timdown/rangy/wiki/Class-Applier-Module
rangy.createClassApplier(String theClass[, Object options[, Array tagNames]])
可靠地找到由 execCommand
创建的元素的一种方法是比较运行之前和之后的子元素列表。任何在调用 execCommand
之前不存在的元素都是由 execCommand
添加的。
以下是一个例子:
// `container` is the contenteditable container element
// Get all the existing elements
const elementsBefore = Array.from(container.querySelectorAll("*"));
// Run the command (wrap selection in a p tag)
document.execCommand("formatblock", false, "p");
// Get all the existing elements again
const elementsAfter = Array.from(container.querySelectorAll("*"));
// Find any that elements didn't exist before `execCommand` and select the first one
const newElement = elementsAfter.filter(e => !elementsBefore.includes(e))[0]
// `newElements` should now be the p tag that was added
p
。 - Timwindow.getSelection().focusNode
而不是带有parentNode
。 - Ahtisham