简体   繁体   English

每次在 Javascript 中单击同一个按钮时,如何用新文本替换文本?

[英]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.

相关问题 当仅在新窗口中使用javascript并在同一选项卡中使用另一个javascript单击单选按钮时,如何打开多个网页 - How can I open multiple webpages when radio button is clicked using only javascript one in new window and other in same tab 每次单击时如何更改按钮的文本值? - How to change the text value of the button everytime it is being clicked? 每次单击提交按钮时,如何执行新的AJAX? - How do you execute a new AJAX everytime a submit button is clicked? 单击时用文本/代码替换 HTML 按钮 - Replace HTML button with text/code when clicked 如何在 html 中创建一个按钮,在单击时更改其文本,并在我再次进入页面时保存新文本? - How can i create a button in html that changes its text when clicked, and the new text is saved when i enter the page again? 单击按钮时的新文本字段 - New Text field when Button is clicked 在 HTML 中使用 JavaScript 单击时如何切换按钮文本 - How to toggle button text when clicked with JavaScript in HTML 如何更改文本的颜色,并在javascript中单击4次按钮时停止 - How to change color of text and stop when clicked the button with 4 times in javascript ValidateForm 如何在 JavaScript 中单击提交按钮时验证和显示文本 - ValidateForm How to validate and show text when submit button was clicked in JavaScript 在 JavaScript 中单击提交按钮时如何更改段落的文本 - How to change text of a paragraph when submit button is clicked in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM