Javascript execCommand头不工作

Javascript execCommand头不工作,javascript,html,rich-text-editor,execcommand,Javascript,Html,Rich Text Editor,Execcommand,正如标题所解释的,由于某种原因,document.execCommand('heading',false,'h1')(或任何其他标题大小)不起作用。但是,其他命令,如'bold','link',等等都可以工作 我正在研究,它似乎得到了全面的支持,尽管存在“0个已知问题” 下面是我正在使用的代码。值得注意的是,我尝试嵌入一个演示,甚至创建了一个代码笔,但由于某些原因,这两个命令都不能工作 const formatToolClass = document.getElementsByClassName

正如标题所解释的,由于某种原因,
document.execCommand('heading',false,'h1')
(或任何其他标题大小)不起作用。但是,其他命令,如
'bold'
'link'
,等等都可以工作

我正在研究,它似乎得到了全面的支持,尽管存在“0个已知问题”

下面是我正在使用的代码。值得注意的是,我尝试嵌入一个演示,甚至创建了一个代码笔,但由于某些原因,这两个命令都不能工作

const formatToolClass = document.getElementsByClassName('format-tool')

const preventDefault = (e) => e.preventDefault()

const applyStyle = function(){
  document.designMode = 'on'
  document.execCommand('heading', false, 'h1') // does not work
  document.execCommand('bold', false, null) // will work
  document.designMode = 'off'
}

for (let i = 0; i < formatToolClass.length; i++){
  formatToolClass[i].addEventListener('mousedown', preventDefault)
  formatToolClass[i].addEventListener('click', applyStyle)
}
const formatToolClass=document.getElementsByClassName('format-tool'))
const preventDefault=(e)=>e.preventDefault()
常量applyStyle=函数(){
document.designMode='on'
document.execCommand('heading',false,'h1')//不起作用
document.execCommand('bold',false,null)//将起作用
document.designMode='off'
}
for(设i=0;i
知道发生了什么事吗??

下面是HTML和CSS,以防你想在浏览器中乱用

<!DOCTYPE html>
  <head>
    <title>Is required and cannot be empty.</title>
    <meta charset="UTF-8">\
    <link rel="stylesheet" href="css/write.css">
  </head>
  <body>
    <div id="controls">
      <div class="controls format-tool" id="controls-embolden" data-xcom="bold">Embolden</div>
      <div class="controls format-tool" id="controls-italicize" data-xcom="italic">Italicize</div>
      <div class="controls format-tool" id="controls-heading" data-xcom="heading">Heading</div> <!---->
      <div class="controls format-tool" id="controls-link" data-xcom="createLink">Link</div> <!---->
      <div class="controls format-tool" id="controls-list" data-xcom="insertUnorderedList">List</div>
      <div class="controls format-tool" id="controls-image" data-xcom="insertImage">Image</div> <!---->
      <div class="controls" id="controls-shortcuts">Shortcuts</div>
      <div class="controls" id="controls-save">Save</div>
      <div class="controls" id="controls-publish">Publish</div>
    </div>
    <div id="user-input" contenteditable="true">
      By portraying me in a sexual way to attendees, this would have opened the door to additional harassment, and added yet another hurdle I’d have to overcome in order to be perceived as a competent professional. The fact that nowhere along the way did ReactiveConf organizers recognize how this gift could actually be harmful, to me, demonstrated a complete lack of empathy for women in tech. That’s not an organization I want to associate myself or my employer, Meteor Development Group (MDG), with.

      Not only is the photo itself problematic, but also the fact that ReactiveConf never asked for my consent. I was never informed that ReactiveConf was planning on altering my photo for the event, nor did I see the superhero picture until after day one of the conference had ended. None of the organizers explicitly asked for my permission to display the picture on the big screen. Had I presented my talk on day 1, I would have been completely blindsided as I walked on stage, which is what happened to a colleague of mine who was also unhappy with his picture.

      Speaking at a conference is already a monumental investment of mental, emotional, and physical effort, right up until the plane ride home. The will to continue investing any more energy into an event whose organizers had showed so little consideration for their speakers vanished as soon as I received the gift. After consulting with trusted members of my team, I decided to withdraw and leave the situation immediately.

      By portraying me in a sexual way to attendees, this would have opened the door to additional harassment, and added yet another hurdle I’d have to overcome in order to be perceived as a competent professional. The fact that nowhere along the way did ReactiveConf organizers recognize how this gift could actually be harmful, to me, demonstrated a complete lack of empathy for women in tech. That’s not an organization I want to associate myself or my employer, Meteor Development Group (MDG), with.
    </div>
    <script src="js/write.js"></script>
  </body>
</html>


body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}

#controls {
  width: 100%;
  height: 10%;
  display: flex;
  justify-content: space-around;
box-sizing:border-box;
border:solid red 1px;
}

.controls {
  font-size: 1.5vmin;
  display: flex;
  justify-content: center;
  align-items: flex-end;
box-sizing:border-box;
border:solid orange 1px;
}

#user-input {
  width: 80%;
  height: 90%;
  max-width: 1440px;
  padding: 1.25%;
  overflow-y: visible;
  overflow-x: hidden;
  font-family: arial;
  font-size: 3vmin;
  color: #000;
box-sizing:border-box;
border:solid black 1px;
}

是必需的,不能为空。
\
壮胆
斜体
标题
链接
列表
形象
捷径
拯救
发表
通过在与会者面前以性的方式描绘我,这将为更多的骚扰打开大门,并增加了我必须克服的另一个障碍,以便被视为一名称职的专业人士。事实上,在整个过程中,ReactiveConf的组织者都没有意识到这份礼物实际上对我是有害的,这表明我对科技界的女性完全缺乏同情心。我不想让我自己或我的雇主Meteor Development Group(MDG)联想到这个组织。
不仅照片本身有问题,而且ReactiveConf从未征求过我的同意。我从未被告知ReactiveConf计划更改我在活动中的照片,也没有看到超级英雄的照片,直到会议的第一天结束。没有一个组织者明确要求我允许在大屏幕上展示这张照片。如果我在第一天发表演讲,当我走在舞台上时,我会完全被蒙蔽了双眼,我的一位同事也对他的照片感到不满意。
在一次会议上发言已经是一项巨大的精神、情感和体力投资,直到飞机回家。当我收到礼物时,组织者对他们的演讲者很少关心的继续投入更多精力的意愿就消失了。在与我的团队中值得信赖的成员协商后,我决定退出,并立即离开。
通过在与会者面前以性的方式描绘我,这将为更多的骚扰打开大门,并增加了我必须克服的另一个障碍,以便被视为一名称职的专业人士。事实上,在整个过程中,ReactiveConf的组织者都没有意识到这份礼物实际上对我是有害的,这表明我对科技界的女性完全缺乏同情心。我不想让我自己或我的雇主Meteor Development Group(MDG)联想到这个组织。
身体{
宽度:100vw;
高度:100vh;
溢出:隐藏;
保证金:0;
}
#控制{
宽度:100%;
身高:10%;
显示器:flex;
证明内容:周围的空间;
框大小:边框框;
边框:纯红1px;
}
.控制{
字体大小:1.5vmin;
显示器:flex;
证明内容:中心;
对齐项目:柔性端;
框大小:边框框;
边框:实心橙色1px;
}
#用户输入{
宽度:80%;
身高:90%;
最大宽度:1440px;
填充:1.25%;
溢出y:可见;
溢出x:隐藏;
字体系列:arial;
字体大小:3vmin;
颜色:#000;
框大小:边框框;
边框:纯黑1px;
}

你的假设是错误的。caniuse站点声明支持
execCommand
,没有问题,这是正确的,但这并不意味着支持所有命令。事实上,在注释部分中有一条评论:

要确定支持哪些命令,请参阅
Document.queryCommandSupported()

如果您选中“标题”,您将看到它在Chrome中不受支持:


console.log(document.queryCommandSupported(“heading”)
您的假设是错误的。caniuse站点声明支持
execCommand
,没有问题,这是正确的,但这并不意味着支持所有命令。事实上,在注释部分中有一条评论:

要确定支持哪些命令,请参阅
Document.queryCommandSupported()

如果您选中“标题”,您将看到它在Chrome中不受支持:


console.log(document.queryCommandSupported(“heading”)
信息量很大。我只有足够的时间浏览一遍,但我想这会把工作完成的。我确实记得读过关于
queryCommandSupported
方法的文章,但我一定忘记了,因为我在这个项目上只花了20分钟在这里,20分钟在那里。如果你选择第一个或最后一个单词,出于某种原因,它会将它们从
div#user input
中删除,并将它们放在前面或后面:/how come?另外,你能看看我刚刚发布的另一个问题吗:哇!在第二个例子中,我没有注意到这一点。这太奇怪了,让我看看我是否能以某种方式修复它。出于某种原因,
insertHTML
嵌入了一堆无用的HTML。如果您按几次enter键,它将嵌入类似于



:而且,这很有趣,因为生成第一个和最后一个单词不会结束