javascript - 在 Web 应用程序中使用标签键插入文本并更改文本大小
问题描述
我正在为课程作业制作一个基本的在线编辑界面,我想使用键盘事件。tab 键应该插入文本,这是可行的,但它也应该减小该行上文本的大小。当我使用 getElementById 时,会显示一条错误消息:无法读取 null 的属性“样式”。我该怎么做?
<!DOCTYPE html>
<html>
<head>
<title>Editor</title>
<link rel="stylesheet" href="stylesheet.css">
<link rel="shortcut icon" href="">
</head>
<body>
<aside class="bar">
<div id="heading-container"></div>
</aside>
<div id="inputArea">
<div id="bulletPoints" contenteditable="true">
<div id="divList" contenteditable="true">
<ul class="inputList">
<li id="outlineList"></li>
</ul>
</div>
</div>
</div>
<script>
window.onload = () => {
window.addEventListener("keydown", checkKeyPress);
function checkKeyPress(key){
if (key.keyCode === 9){
key.preventDefault();
document.execCommand("insertText", true, " ");
document.getElementById("inputList").style.fontSize = "smaller";
}
}
};
</script>
</body>
</html>
解决方案
<!DOCTYPE html>
<html>
<head>
<title>Editor</title>
<link rel="stylesheet" href="stylesheet.css">
<link rel="shortcut icon" href="">
</head>
<body>
<aside class="bar">
<div id="heading-container"></div>
</aside>
<div id="inputArea">
<div id="bulletPoints" contenteditable="true">
<div id="divList" contenteditable="true">
<ul class="inputList" id="inputList">
<li id="outlineList"></li>
</ul>
</div>
</div>
</div>
<script>
window.onload = () => {
window.addEventListener("keydown", checkKeyPress);
function checkKeyPress(key){
if (key.keyCode === 9){
key.preventDefault();
document.execCommand("insertText", true, " ");
document.getElementById("inputList").style.fontSize = "smaller";
}
}
};
</script>
</body>
</html>
您缺少 id 作为ul
.
推荐阅读
- python - 使用 pyodbc 与 sql server 2008 r2 连接,SQL Server 不存在或访问被拒绝
- laravel-5.4 - 同时更新2个表列,laravel
- python - 如何在Qtablewidget PYQT中禁用除一列之外的所有列-python
- java - 无法向 Firebase 进行身份验证
- python - 具有列或行最大限制的 Pyspark 数据框
- maven - 如何在 pom.xml 中检索 Maven 内部版本号?
- angular - 如何在Angular 4中使用打字稿基于其他下拉选择值显示下拉值
- c++ - 在 Windows 中运行 .exe 文件时如何防止 ucrtbase.dll 崩溃?
- c - 从文件的 inode 号获取文件名
- dashboard - 如何将现有仪表板复制到 Appdynamics 中的新项目