【发布时间】:2021-08-05 10:02:19
【问题描述】:
点击按钮时如何更改为id="number1"?
数组中的文本也必须改变。
减去 ${count++} 会更改文本。
我希望我可以将 ID 值从 1 更改为 40。
我该怎么办?请帮帮我。
$(() => {
const TITLE_NUM = 1;
let textData = [
"test2",
"test3",
"test4",
"test5",
]
let onSelectFocus = (x) => {
let text = textData[x];
var count = 0;
$(`.text_box #number${count++}`).text(text).fadeIn();
}
for (let i = 0; i < TITLE_NUM; i ++) {
$('.button').on('click', function(){
onSelectFocus(i++);
})
}
})
.text_box{
width: 500px;
height: 300px;
background: orange;
margin: 0 auto;
align-items: center;
justify-content: center;
text-align: center;
display: flex;
}
.button {
width: 50px;
height: 50px;
background: #000;
margin: 0 auto;
color: #fff;
display: flex;
justify-content: center;
margin-top: 40px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body>
<div class="text_box">
<p id="number">test1</p>
</div>
<div class="button"><p>click</p></div>
<script src="main.js"></script>
</body>
</html>
【问题讨论】:
-
但是,没有
number1的元素?你也需要创建它们吗? -
将
var count = 0;放入函数中意味着函数运行时count将始终为0。可能不是你想要的? -
@Swati 对不起。我不知道它是什么。
-
@ChrisG 包含 varcount=0 时没有任何效果。
标签: javascript jquery arrays for-loop