javascript - HTML如何让用户输入粗体或斜体?
问题描述
问题是:我正在开发一个带有粗斜体按钮的简单文本编辑器。我看过很多教程,它们改变了文本的选择。在文本选择过程中,用户首先必须输入非格式化文本,然后选择它,然后单击粗体或斜体按钮来格式化文本。
但是,我想为用户提供打开粗体或斜体按钮的功能,然后在他们键入时完成所有文本格式设置。
我目前正在使用.designMode
打开变量的 iframe。目前,我的实现只支持文本选择格式,但我也想实现实时文本格式。
这是html:
<div id="textEditor">
<div id="ribbon">
<button id="boldButton" title="Bold"><b>B</b></button>
<button id="italicButton" title="Italic"><em>I</em></button>
</div>
<div id="richTextArea">
<iframe id="wysiwyg" name="wysiwyg" frameborder="0"></iframe>
</div>
</div>
这是相应的脚本(javascript):
window.addEventListener("load", function() {
var editor = wysiwyg.document; //wysiwyg iframe has name="wysiwyg"
editor.designMode = "on";
boldButton.addEventListener("click", function() {
//when bold button is clicked, execCommand turns the text selection to bold
editor.execCommand("Bold", false, null);
}, false);
italicButton.addEventListener("click", function() {
//when bold is clicked, bold the selected text
editor.execCommand("Italic", false, null);
}, false);
}, false);
解决方案
推荐阅读
- php - 在关键位置将多个项目插入数组
- arrays - 如何从批处理文件中的一组字符串中设置随机值?
- azure - .net core 5.0 生产是否准备好用于 azure 应用服务
- flutter - 如何将颤振应用程序连接到 aws iot?
- c++ - 错误消息:operator< 不匹配,因为 battery_sub_ 是订阅者,而电池是双倍的
- if-statement - Google 表格中的“适合所有人”
- spring - API 端点的命名约定
- flutter - 参数类型 'BlocBuilder
'不能分配给参数类型'SliverChildDelegate' - android - Kotlin 代码中的 Android Studio 断点不起作用(未找到可执行代码)
- typescript - Vue CLI + Axios TypeError: XX is not a function