简体   繁体   English

如何在鼠标单击上更改元素文本

[英]How to Change Element Text on Mouse Click

So, I'm new to this kind of stuff, and I have a question. 所以,我是这种东西的新手,我有一个问题。 I found this code for picking a random word, but not picking it again until all the word have been picked. 我发现此代码用于选择随机单词,但是直到所有单词都被选中后才再次选择它。

function shuffle(array)
{
  var m = array.length, t, i;
  while (m > 0) 
  {
    i = Math.floor(Math.random() * m--);
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }
  return array;
}

var keywords = ["Cheese", "Apples", "Grapes", "Chicken", "Potatoes", "Beef", "Bananas"];

shuffle(keywords); // shuffles the array

alert(keywords);

And that brought up a question. 这就提出了一个问题。 How can I trigger this when a white box, posing as a card, has been clicked? 单击冒充卡片的白框时如何触发? I would like a box to display only one of the values at any one time, and randomly pick another word when clicked, using this code. 我希望一个框在任何时间仅显示一个值,并在单击时使用此代码随机选择另一个单词。

Any help is super very much appreciated. 非常感谢任何帮助。

There no need to use a shuffle, you can pick a random item using something like 无需使用随机播放,您可以使用类似

 var keywords = ["Cheese", "Apples", "Grapes", "Chicken", "Potatoes", "Beef", "Bananas"]; var mybox = document.getElementById('mybox'), idx; //click handler for the box mybox.addEventListener('click', function() { var index; do { index = Math.floor(Math.random() * keywords.length); } while (index == idx); //to make sure every click changes the selcted value mybox.innerHTML = keywords[index]; idx = index; }) 
 #mybox { border: 1px solid black; height: 100px; width: 100px; text-align: center; } 
 <div id="mybox"></div> 

Make an html tag like so: 制作一个html标记,如下所示:

<div id="myvalue">default value</div>

For the sake of the example I'm going to use a button as a mock for your 'white box' since I don't know what the html behind it is. 对于本示例,我将使用按钮作为“白盒”的模拟,因为我不知道它背后的html是什么。 However, the same principle applies. 但是,同样的原则适用。

<button id="myButton">Mock representing the "white box"</button>

Then create a function that randomly selects a value from the list and displays it in the tag above: 然后创建一个函数,从列表中随机选择一个值并将其显示在上面的标签中:

function showWord(e){
    var len = 1/keywords.length;
    var pos = Math.floor(Math.random()/len);
    document.getElementById('myvalue').innerHTML = keywords[pos];
}

...and bind it to the display-triggering element (in this case the button, in yours, whatever tag the "card" is made up) with a click listener: ...并使用点击侦听器将其绑定到触发显示的元素(在这种情况下,就是您自己的按钮,即“ card”的任何标记)。

document.getElementById('myButton').addEventListener("click", showWord);

Clicking the button (or whatever you bind the event listener to) will now cause a random selection from the array to show up in the div. 单击按钮(或将事件侦听器绑定到的任何按钮)现在将导致从数组中随机选择,以显示在div中。 Each subsequent click will change it. 随后的每次点击都会对其进行更改。

Here's a jsfiddle showing it in action 这是一个jsfiddle显示它的作用

(Your question was a bit vague, so it's not a perfect answer, but hopefully it gives you what you need. Also, I'm running under the assumption that you already have a specific 'white box posing as a card' element created or at least in mind, and need the Javascript to go along with it) (您的问题有点含糊,所以这不是一个完美的答案,但希望它能为您提供所需的信息。此外,我假设您已经创建了一个特定的“白盒子冒充卡”元素,或者至少要记住,并且需要Javascript才能配合使用)

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

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