gpt4 book ai didi

javascript - execCommand 标题不起作用

转载 作者:行者123 更新时间:2023-12-01 02:46:41 26 4
gpt4 key购买 nike

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

我正在看caniuse尽管存在“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)
}

知道发生了什么吗?

附注下面是 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;
}

最佳答案

你做出了错误的假设。 caniuse 网站指出 execCommand没有问题地受到支持,这是正确的,但这并不意味着所有命令都受支持。事实上,caniuse page注释部分有评论:

To determine what commands are supported, see Document.queryCommandSupported()

如果您检查“标题”,您会发现 Chrome 不支持它:

console.log(document.queryCommandSupported("heading"))

IE 也不支持。 Firefox 声明它支持它,但它可能会或可能不会按照您期望的方式:它不是将标题样式仅应用于选定的文本,而是将其应用于整个 block (就像“formatBlock”一样,其意义在于标题是 block 级元素,而不是像粗体/斜体那样的内联元素)。

如果这对您有用,有一个解决方法是使用“formatBlock”,因为它受 Chrome、Firefox 和 IE 支持:

console.log(document.queryCommandSupported("formatBlock"))

在这种情况下,您要做的就是将 formatBlock 设置为您想要的标题(看起来像 h1 ),它将起作用:

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

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

const applyStyle = function() {
document.designMode = 'on'
document.execCommand('formatBlock', false, 'h1')
document.designMode = 'off'
}

for (let i = 0; i < formatToolClass.length; i++) {
formatToolClass[i].addEventListener('mousedown', preventDefault)
formatToolClass[i].addEventListener('click', applyStyle)
}
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;
}
<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>

<小时/>

现在,如果您只想将您选择的文本作为 H1,那么您可以使用一个技巧:使用“insertHTML”添加标签 <h1></h1>包围所选文本(它适用于 Edge,但不适用于 Internet Explorer)。像这样的事情:

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

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

const applyStyle = function() {
document.designMode = 'on'
document.execCommand('insertHTML', false, '<h1>' + window.getSelection().toString() + '</h1>')
document.designMode = 'off'
}

for (let i = 0; i < formatToolClass.length; i++) {
formatToolClass[i].addEventListener('mousedown', preventDefault)
formatToolClass[i].addEventListener('click', applyStyle)
}
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;
}
<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>

关于javascript - execCommand 标题不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47213023/

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