gpt4 book ai didi

javascript - ExecCommand 不加粗

转载 作者:行者123 更新时间:2023-11-30 17:41:21 33 4
gpt4 key购买 nike

使用基于选择/键盘命令所见即所得 编辑器。效果很好,除了...

与所有其他键盘命令一起,它可以打开和关闭特定样式 I、打击等。

使用 execCommand('bold'),它不会取消加粗文本。这非常令人沮丧。每个浏览器都一样。

这是代码。

$('body').on('keydown', '.element_content, .element_content_subhead', function(e) { 

if(e.ctrlKey){

//bold -- key 'B'
if(e.which == 66){
$sel = $.trim(document.getSelection().toString());
if($sel == ''){
alert('Please select some text to bold');
}
else{
document.execCommand('bold', false, null);
}
}
}

});

最佳答案

这是我的答案。

因为我使用的是非标准字体 (ClanWeb),所以加粗字体的 b 在浏览器中效果不佳。所以在我的 CSS 中我有 b{ font-family:ClanWeb-bold;字体粗细:正常!重要;

这对于加粗类型来说效果很好,但是如果标签的行为不正常,则 execCommand 不会触发; CSS 中没有 font-weight:bold。

所以这是我的代码来取消加粗。

  //bold -- key 'B'
if(e.which == 66){
$sel = $.trim(document.getSelection().toString());

var parentEle = window.getSelection().getRangeAt(0).commonAncestorContainer;
parentEle = parentEle.parentNode;


if($sel == ''){
alert('Please select some text to bold');
}
else if(parentEle.tagName == 'B'){
parentEle.id='unbold1';
$('#unbold1').contents().unwrap();
}
else{
document.execCommand('bold', false, null);
}
}

关于javascript - ExecCommand 不加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21030120/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com