简体   繁体   English

如何只更改“inner.html”的一部分?

[英]How to change only a part of an “inner.html”?

I have this counter that based on the requirements - Counts backwards or in ascending order and It works just fine however I want to display text along with each of the numbers as they are shown being counted - however the text changes as soon as there is a change.我有这个计数器,它根据要求 - 向后或按升序计数,它工作得很好但是我想显示文本以及每个数字,因为它们正在被计数 - 但是一旦有一个文本就会改变改变。 So the question is - How can I have a innerhtml that only affects a part of it not the full content.所以问题是 - 我怎样才能有一个只影响它的一部分而不是全部内容的 innerhtml。 I want the text to remain as it is.我希望文本保持原样。

here is the html and JS.这里是 html 和 JS。

 <div class="container">
  <div class="number mt-3">100 **[I WANT TO ADD SOME TEXT HERE]** </div>
   
</div>

JS JS

    const numberEl = document.querySelector('.number');
let startNumber = parseInt(numberEl.innerHTML);
const endNumber = 0;

const ticker = setInterval(() => {
  numberEl.innerHTML = startNumber--;
  if(startNumber === endNumber - 1) {
    clearInterval(ticker);
  }
}, 200);

You can also find the codepen here: https://codepen.io/kenkozma17/pen/XWdbdrd您还可以在此处找到代码笔: https://codepen.io/kenkozma17/pen/XWdbdrd

You can use += to append HTML and use <br> for a line break.您可以使用 += 到 append HTML 并使用<br>换行。

numberEl.innerHTML += "<br>" + startNumber--;

To only change the count down number, you could enclose it inside a span, leaving you free to modify other text in the div.要仅更改倒计时数字,您可以将其包含在一个跨度内,让您可以自由修改 div 中的其他文本。

<div class="number mt-3"><span>100</span> **[I WANT TO ADD SOME TEXT HERE]** </div>
<script>
numberEl.querySelector('span').innerHTML  = startNumber--;
<script>

You could also do something like this:你也可以这样做:

 <div class="container"> <h2>This number will increment from 100 to 200</h2> <p><span class="number mt-3">100</span>Some text</p> </div>

You can try this:你可以试试这个:

 const numberEl = document.querySelector('.number'); let startNumber = parseInt(numberEl.innerHTML); let extraText = document.querySelector('.number span').innerText; const endNumber = 200; const ticker = setInterval(() => { numberEl.innerHTML = (startNumber++) + extraText; if(startNumber === endNumber + 1) { clearInterval(ticker); } }, 100);
 .container { text-align: center; margin-top: 5em; }.number { font-weight: 500; font-size: 3em; }
 <div class="container"> <h2>This number will increment from 100 to 200</h2> <div class="number mt-3">100<span>&nbsp;&nbsp;Your Text</span></div> </div>

Okay, i'll make my comments an answer too;)好的,我也会让我的评论成为答案;)

@MohammadQasim here is the idea of adding a string to the counter;) http://codepen.io/gc-nomade/pen/OJNympB – G-Cyrillus 7 mins ago @MohammadQasim 这里是向计数器添加字符串的想法;) http://codepen.io/gc-nomade/pen/OJNympB – G-Cyrillus 7 分钟前

or @MohammadQasim http://codepen.io/gc-nomade/pen/yLOYboL ;) if you need the text from HTML – G-Cyrillus 4 mins ago或@MohammadQasim http://codepen.io/gc-nomade/pen/yLOYboL ;) 如果您需要 HTML – G-Cyrillus 4 分钟前的文本

  • add another string with innerHTML用 innerHTML 添加另一个字符串

 const numberEl = document.querySelector('.number'); let startNumber = parseInt(numberEl.innerHTML); let txt=" My Text"; const endNumber = 200; const ticker = setInterval(() => { numberEl.innerHTML = startNumber++ + txt; if(startNumber === endNumber + 1) { clearInterval(ticker); } }, 100);
 .container { text-align: center; margin-top: 5em; }.number { font-weight: 500; font-size: 3em; }
 <div class="container"> <h2>This number will increment from 100 to 200</h2> <div class="number mt-3">100 </div> </div>

  • Or if you want to retrieve it from your HTML template:或者,如果您想从 HTML 模板中检索它:

 const numberEl = document.querySelector('.number'); let startNumber = parseInt(numberEl.innerHTML); let txt=document.querySelector('.number span').textContent; const endNumber = 200; const ticker = setInterval(() => { numberEl.innerHTML = startNumber++ + txt; if(startNumber === endNumber + 1) { clearInterval(ticker); } }, 100);
 .container { text-align: center; margin-top: 5em; }.number { font-weight: 500; font-size: 3em; }
 <div class="container"> <h2>This number will increment from 100 to 200</h2> <div class="number mt-3">100 <span> text I want to keep & see </span></div> </div>

I will give the idea, Just separate your text in another Div with ID.我会给出这个想法,只需将您的文本分隔在另一个带有 ID 的 Div 中。

<div class="container">
  <div class="number mt-3">
     100        
  </div>
  <div id="changeSubContent">
    **[I WANT TO ADD SOME TEXT HERE]**
  </div>
</div>

Then you can change the sub-content:然后您可以更改子内容:

document.getElementById("changeSubContent").innerHTML = "Your Text";

So you can customize the contents in each div as you want.因此,您可以根据需要自定义每个 div 中的内容。

You might look at the .insertAdjacentHTML method if you just want to add some content at the beginning or end of an element.如果您只想在元素的开头或结尾添加一些内容,您可能会查看.insertAdjacentHTML方法

Or if you want to be able to target specific areas within your div repeatedly, you might consider adding dedicated elements inside the div to hold specific pieces of content, as shown in this demo code:或者,如果您希望能够重复定位 div 中的特定区域,您可以考虑在 div 中添加专用元素来保存特定的内容,如以下演示代码所示:

 const numSpan = document.getElementById("numSpan"), textSpan = document.getElementById("textSpan"); let countdownNumber = parseInt(numSpan.textContent); const endNumber = 0; const ticker = setInterval(() => { numSpan.textContent = countdownNumber--; if (countdownNumber === endNumber - 1) { showBlastoff(); clearInterval(ticker); } }, 200); function showBlastoff() { textSpan.textContent = "-- Blastoff;!!"; }
 <div class="container"> <div class="number mt-3"> <span id="numSpan">30</span> <span id="textSpan">... and counting down...</span> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM