简体   繁体   English

使用 HTML/Javascript 在点击时存储元素?

[英]Storing elements on click with HTML/Javascript?

so Im a noob at html css and javascript, so I this is probably a really obvious question that would be easy to find the answer to with google.所以我是 html css 和 javascript 的菜鸟,所以我这可能是一个非常明显的问题,很容易用谷歌找到答案。 Alas, I dont really know where/how to look up what I want, so any tips on finding out how to do specific tasks would be appreciated唉,我真的不知道在哪里/如何查找我想要的东西,所以任何有关找出如何执行特定任务的提示都将不胜感激

Basically, I want to know how to give the users an option to click on a word on a list (or something like a star icon beside the word) so that the word/element can be stored in a "favorites" tab.基本上,我想知道如何让用户选择单击列表中的某个单词(或单词旁边的星形图标之类的东西),以便可以将单词/元素存储在“收藏夹”选项卡中。

Sorry again Im really bad at this.再次抱歉,我真的很不擅长这个。

So basicaly you want to get the text of the element and add it in a array.所以基本上你想要获取元素的文本并将其添加到数组中。 Like this :像这样 :

html html

<html>
  <head>

  </head>
  <body>
    <div id="scan">
      <span>1</span>
      <span>2</span>
      <span>3</span>
      <span>4</span>
    </div>
    <span id="needText">tralala</span>
  </body>
</html>

Javascript : Javascript :

var array = [];
var textSolo = document.getElementById('needText');
var scan = document.getElementById('scan');

textSolo.onclick = function(){getClick(event)};
scan.onclick = function(){getClick(event)}

function getClick(text){
    array.push(text.target.textContent);
}

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

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