如何使用document.execCommand
将字体大小更改为30px(例如)?
这个:
document.execCommand("fontSize", false, "30px");
不起作用,因为在函数execCommand的第三个参数中,它只允许我在1到7之间输入一个值。
这是FontSize
命令的限制。我能想到各种各样的选择:
document.execCommand("fontSize", false, "7");
,然后查找命令创建的元素并根据需要更改它们。见例子:http://jsfiddle.net/S3ctN/。这显然取决于文档中没有其他大小为7的<font>
元素,它还依赖于浏览器使用<font>
元素来显示字体大小,这似乎都是这样。功能
var execFontSize = function (size, unit) {
var spanString = $('<span/>', {
'text': document.getSelection()
}).css('font-size', size + unit).prop('outerHTML');
document.execCommand('insertHTML', false, spanString);
};
执行
execFontSize(30, 'px');
第二个答案建议在execcommand之后运行一些jquery并用自己的标记替换标记。
只需将elem替换为您的元素,然后使用所需的值编辑font-size。
jQuery("font[size]", elem).removeAttr("size").css("font-size", "10px");
$(document).ready(()=>{
var activeFontSize = 30;
var oDoc = document.getElementById("editor");
var style = document.createElement("style");
document.body.appendChild(style);
function setFontSize(value){
$editor.focus();
document.execCommand("fontsize", false, 20);
activeFontSize = value;
createStyle();
updateTags();
}
function updateTags(){
var fontElements = oDoc.getElementsByTagName("font");
for (var i = 0, len = fontElements.length; i < len; ++i) {
if (fontElements[i].size == "7") {
fontElements[i].removeAttribute("size");
fontElements[i].style.fontSize = activeFontSize+"px";
}
}
}
function createStyle(){
style.innerHTML = '#editor font[size="7"]{font-size: '+activeFontSize+'px}';
}
function updateToolBar(args){
$fontSize.val(args.fontsize);
}
var $fontSize = $("#fontSize");
var $editor = $("#editor");
$fontSize.on("change", ()=>{
setFontSize($fontSize.val())
})
$editor.on("keyup", ()=>{
updateTags();
})
//var i = 0;
$editor.on("keyup mousedown", (e)=>{
//i++;
//console.log("e.target", e.target)
try{
var fontsize = $(window.getSelection().getRangeAt(0).startContainer.parentNode).css("font-size")
fontsize = fontsize.replace("px", "");
//document.execCommand("insertHTML", false, '<span class="font-size-detector'+i+'">X</span>');
//var fontsize = $(e.target).css("font-size")//$editor.find(".font-size-detector"+i).css("font-size");
//document.execCommand("undo", false, false);
//$editor.focus();
//console.log("fontsize", fontsize)
updateToolBar({fontsize})
}catch(e){
console.log("exception", e)
}
})
oDoc.focus();
setFontSize(30);
})
.editor-box{box-shadow:0px 0px 1px 2px #DDD;max-width:500px;margin:0px auto;}
.editor-tools{padding:20px;box-shadow:0px 2px 1px 0px #DDD}
.editor-tools button{user-select:none;}
#editor{height:200px;margin:10px 0px;padding:10px;font-size:14px;}
#editor:focus{outline:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="editor-box">
<div class="editor-tools">
<label>Font Size: </label>
<select id="fontSize" >
<option value="10">10px</option>
<option value="12">12px</option>
<option value="14">14px</option>
<option value="20">20px</option>
<option value="22">22px</option>
<option value="30" selected="true">30px</option>
<option value="35">35px</option>
<option value="40">40px</option>
<option value="45">45px</option>
<option value="50">50px</option>
</select>
</div>
<div id="editor" contenteditable="true">Hello, this is some editable text</div>
</div>
它只是我的解决方案。它适用于chrome。代码在中国网站(eqxiu)中找到。
第一
document.execCommand("styleWithCSS", 0, true);
document.execCommand('fontSize', 0, '12px');
然后
jQuery.find('[style*="font-size: -webkit-xxx-large"],font[size]').css("font-size", "12px")
注意:执行execCommand的fontsize传递必须至少为'12px'及以上。
只需用css覆盖它:
font[size='7']{
font-size: 20px; // or other length unit
}
或者如果你使用scss,你可以使用循环来生成从1到7的所有选择器:
@for $i from 1 to 7 {
font[size='#{$i}']{
font-size: $i * 2vw
}
}
这里有一个比这里提出的更好的解决方案,因为它直接获取了从所选文本更改字体大小的元素。
因此,它不必浏览DOM以获取正确的元素,也不需要禁止使用特定的fontSize(7,如接受的答案的第一个选项中所建议的,以及其他答案)。
function changeFont() {
document.execCommand("fontSize", false, "7");
var fontElements = window.getSelection().anchorNode.parentNode
fontElements.removeAttribute("size");
fontElements.style.fontSize = "30px";
}
总之,我们只使用execCommand用span来包装选择,以便后续调用getSelection()将突出显示的span作为父级。然后我们只将fontSize样式添加到该标识的范围。
在这里,当我们使用fontSize命令时,它不会被包装在<span>
中,而是包含在<font>
中。
但这是一个通用方法,可以使用execCommand的任何命令,后者仅用作方便的方式来包装所选内容,甚至在不同的元素之间。
这是一个现场DEMO
在纯JavaScript中找到了一个解决方案,该解决方案适用于具有自定义HTML(颜色,字体系列)的多行。
获取文档的选择。解析选择并从中保存html标记。然后使用document.execCommand将选定的html插入到具有内联样式的div中。使用document.execCommand('insertHTML',false,html)时的一个好处是你可以在WYSIWYG编辑器中撤消。
这是功能:
function fontSize(size) {
var sel = document.getSelection(); // Gets selection
var selectedHtml = "";
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
selectedHtml = container.innerHTML;
}
let html = `<div style="font-size: ${size}px;">${selectedHtml}</div>`
document.execCommand('insertHTML', false, html);
}
希望能帮助到你。