javascript - 我的构造函数有问题
问题描述
嗨,我想在按下按钮时将文本从变量添加到 div。但我被卡住了。文本添加在底部或根本不添加。
var text=['text1','text2','text3'];
var title=['title1','title2','title3'];
var i=0;
$("#Construct").on("click",function()
{
newTitle = document.createElement("div");
newTitle.innerHTML = title[i];
newTitle.classList.add("title")
newText = document.createElement("div");
newText.innerHTML = text[i];
newText.classList.add("text")
insertplace = document.getElementById("#insert");
document.body.insertBefore(newText, insertplace.nextSibling);
document.body.insertBefore(newTitle, insertplace.nextSibling);
i++;
});
#example{
background: lightblue;
width: 400px;
}
.text {
color: black;
font-family: Verdana;
font-size: 16px;
text-align: left;
}
.title {
color: black;
font-family: Verdana;
font-size: 28px;
text-align: right;
}
#created{
background: green;
width: 400px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="Construct" class="button" type="button" value="Construct" >
<div id="example">
<div class="title">
Title
</div>
<div class="text">
Text
</div>
</div>
<br>
<div id="created">
<div id="insert"> </div>
</div>
我也可以这样创建程序按钮吗?我想让他们在字符串变量中有文件路径的同时播放声音文件。
解决方案
.getElementById()
意味着您将传递一个id
,因此您不添加“#”。
此外,代替.insertBefore()
,.appendChild()
在插入区域使用。
纠正这些问题后,当您第 4 次单击按钮时,您仍然会遇到问题,因为您的数组只有 3 个项目,因此需要在增加变量之前检查变量。
let insertPlace = document.getElementById("insert");
var text=['text1','text2','text3'];
var title=['title1','title2','title3'];
var i=-1; // Start before the first index
$("#Construct").on("click",function(){
// Check to see if there is another item in the array
if(i < text.length -1) {
i++;
} else {
console.log("There are no more items to create elements from.");
return; // exit the function
}
newTitle = document.createElement("div");
newTitle.innerHTML = title[i];
newTitle.classList.add("title")
newText = document.createElement("div");
newText.innerHTML = text[i];
newText.classList.add("text")
// With .getElementById(), you don't add "#"
insertplace = document.getElementById("insert");
insertPlace.appendChild(newText);
insertPlace.appendChild(newTitle);
});
#example{
background: lightblue;
width: 400px;
}
.text {
color: black;
font-family: Verdana;
font-size: 16px;
text-align: left;
}
.title {
color: black;
font-family: Verdana;
font-size: 28px;
text-align: right;
}
#created{
background: green;
width: 400px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="Construct" class="button" type="button" value="Construct" >
<div id="example">
<div class="title">Title</div>
<div class="text">Text</div>
</div>
<br>
<div id="created">
<div id="insert"> </div>
</div>
推荐阅读
- php - 如何使用 SQL LIKE 运算符在 PHP 中使用 PDO 准备语句
- php - 睡眠进程过多(MySQL)
- python - 使用 AWS ALB / Cognito 进行身份验证
- postgresql - SELECT DISTINCT 只能出现一次的行
- python - pip install 从哪个 url 获取它的文件?
- image-processing - iiifhosting 站点如何将图像处理为向上/向下缩放以使其与 iiif 兼容?
- reactjs - React useEffect hook 在调度动作时禁用额外的渲染
- javascript - $page.props.csrf_token 是 undefiend (inertiajs)
- python - 尝试从本地字典中查找要发布的图片时出现 Discord.py bot 错误
- node.js - Nginix入口的Websocket问题