简体   繁体   English

如何将动态文本添加到HTML标签?

[英]How do I add dynamic text to a HTML tag?

I am new to JS. 我是JS新手。 I want to add (elements) words from an array on mouse click into HTML. 我想从鼠标单击数组中的数组中添加(元素)单词。

 var pos = document.querySelector('span'); for (let i = 0; i < array length; i++) { button.addEventListener('click', function() { pos.innerHTML = array[i]; }); } 
 <p> the word generated is <span></span></p> <button>go</button> 

 let array1 = ['1', '2', '3']; var pos = document.getElementById('output'); var button = document.getElementById('btn'); button.addEventListener('click', function() { for (var i = 0; i < array1.length; i++) { pos.innerHTML = pos.innerHTML + array1[i]; } }); 
 <p>the word generated is <span id="output"></span></p> <button id="btn">go</button> 

This will display all element of array on button click. 单击按钮将显示数组的所有元素。

Try this, it randomly displays a word in span element. 尝试此操作,它将在span元素中随机显示一个单词。

 var words = ['rest', 'fever', 'ball', 'sky', 'earth', 'moon', 'sun', 'temper']; var button = document.querySelectorAll('button').item(0); button.addEventListener('click', function(event){ let span = document.querySelectorAll('p span').item(0); let randomWord = words[Math.round(Math.random() * (words.length - 1))]; span.textContent = randomWord; }); 
 p span { font-weight: bold; color: red; } 
 <p> the word generated is <span></span></p> <button>go</button> 

Or with Jquery: 或使用Jquery:

<p> the word generated is <span></span></p>
<button id="btn">go</button>

Code: 码:

var array = ['test','test2'];

  $('#btn).on('click', function () {
  array.forEach(function(entry) {
  $('p span).append(entry+' ');
})
})

If you are looking for random text generations, you can use random function to generate random array indexes. 如果要查找随机文本,可以使用random函数生成随机数组索引。

button.addEventListener('click', function() {
  pos.innerHTML = arr[Math.floor(Math.random() * Math.floor(arr.length))]
});

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

相关问题 如何向HTML标记添加属性 - How do I add attributes to an HTML tag jQuery:.html()替换div标签中的文本,但如何添加而不是替换? - Jquery: .html() substitute the text in a div tag, but how do I add to instead of substitute? 如何将javascript变量添加到HTML图像src标记 - How do I add a javascript variable to HTML image src tag 如何通过JQuery向段落标签添加文本? - How do I add text to a paragraph tag via JQuery? 如何获得动态表单来更新HTML中的文本? - How do I get a dynamic form to update Text in HTML? 如何在html文本区域的开头添加默认文本? - How do I add a default text to the beginning of an html text area? 如何添加<script> tag as a simple string without it being treated as a regular <script> tag in HTML? - How do I add a <script> tag as a simple string without it being treated as a regular <script> tag in HTML? 我如何转义动态文本<title>使用 Nuxt JS 在标头内标记? - How do I escape dynamic text in <title> tag inside header using Nuxt JS? 如何从字符串而不是文件向头标记添加动态javascript? - How do I add dynamic javascript to the head tag from a string, not a file? 如何将动态html添加到jQuery对话框的文本字段中? - How do you add dynamic html to a jQuery dialog's text field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM