[英]How do I replace the text with new text everytime when one same button is clicked in Javascript?
JS: JS:
function changeText() {
let str = document.getElementById("font").innerHTML;
document.getElementById("font").innerHTML = str.replace("Dog", "Cat");
let str = document.getElementById("font").innerHTML;
document.getElementById("font").innerHTML = str.replace("Cat", "Mouse");
let str = document.getElementById("font").innerHTML;
document.getElementById("font").innerHTML = str.replace("Mouse", "Rabbit");
let str = document.getElementById("font").innerHTML;
document.getElementById("font").innerHTML = str.replace("Rabbit", "Horse");
}
HTML: HTML:
<button onclick="changeText()">Click</button>
The situation is that I have one button and when I press this button, I would like it to change the name of the new animal to the index.html file every time.情况是我有一个按钮,当我按下这个按钮时,我希望它每次都将新动物的名称更改为 index.html 文件。 How can I do this most easily?我怎样才能最轻松地做到这一点? Thanks for the answers I am a beginner.感谢您的回答我是初学者。
You could do something like this:你可以这样做:
const animals = ['Dog', 'Cat', 'Mouse', 'Rabbit', 'Horse'];
let currentAnimal = 0;
function changeText() {
currentAnimal = (currentAnimal + 1) % animals.length;
document.getElementById("font").innerHTML = animals[currentAnimal];
}
var list = ["Dog","Cat","Mouse","Rabbit","Horse"]
var current = 0
function changeText() {
let str = document.getElementById("font").innerHTML;
document.getElementById("font").innerHTML = list[(current+1)%list.length]
current+=1
}
<button onclick="changeText()">Click</button>
Using the modulus %
operator between the current index and array length, you can ensure cyclic behavior, once all the values in the list
array are traversed.在当前索引和数组长度之间使用模数%
运算符,您可以确保循环行为,一旦遍历list
数组中的所有值。
You could do a replacement table你可以做一个替换表
let replacements = ['Dog', 'Cat', 'Mouse', 'Rabbit', 'Horse'];
function changeText() {
let e = document.getElementById("font");
for (let i = 0; i < replacements.length - 1; i++) {
let c = e.innerHTML.replace(replacements[i], replacements[i+1]);
if (c !== e.innerHTML) { //something has been replaced
e.innerHTML = c;
break; //stop replacing, because otherwise you will always end up with Horse
}
}
}
This will alsow work, if the element's content isn't just the animals name, but for instance a sentence containing the element ...这也将起作用,如果元素的内容不仅仅是动物名称,而是例如包含元素的句子......
Also be aware that string.replace(old, new)
will only replace the first occurence of old
within the string ...还要注意string.replace(old, new)
只会替换字符串中第一次出现的old
......
Hello and good on you for learning a new skill.你好,很高兴你学习了一项新技能。
a good method to implement this is by using an array and counter实现这一点的一个好方法是使用数组和计数器
// Array of animals
let animals = [
'Dog',
'Cat',
'Mouse',
'Rabbit',
'Horse'
]
// Counter to select an index in array
let counter = 0
// function to change text
function changeText() {
// select the word from array
const word = animals[counter]
// select the html element you want to change
// querySelector uses css3 selectors
document.querySelector('.str').textContent = word
// increment counter
counter++;
// check if counter is greater than or equal to array length - 1 because indexes start from 0
// if true reset counter to 0
if (counter >= animals.length - 1) {
counter = 0;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.