简体   繁体   English

如何将javascript应用于html文档中的多个元素?

[英]How to apply javascript to more than one element in html document?

I just started learning HTML and Javascript, so this might turn out to be a dumb question, but I couldn't find the answer anywhere where google sent me. 我刚刚开始学习HTML和Javascript,所以这可能真是一个愚蠢的问题,但是我在Google向我发送邮件的任何地方都找不到答案。

What I want to do: 我想做的事:

I want to have something in my HTML document that takes an argument (a date), runs my javascript on it and then changes the colour and the text according to the argument. 我想在HTML文档中添加一些参数,该参数接受一个参数(日期),在其上运行我的JavaScript,然后根据该参数更改颜色和文本。

What I have tried so far: 到目前为止我尝试过的是:

Working (but extremely stupid) solution: 可行的(但非常愚蠢的)解决方案:

  <p id="demo"> </p> 
  <script>
   d = new Date();
   i = new Date(2014,0,19); //This is my "argument"
   var timesince = Math.floor((d.getTime() - i.getTime())/1000/60);
   var unit = "min";
   var colour = '#AAA';
   if (timesince > 60){
     timesince = Math.floor(timesince/60);
     unit = "h";
     if (timesince > 7) colour = '#a46744';
   }
   document.getElementById("demo").innerHTML = timesince.toString()+unit;
   document.getElementById("demo").style.color = colour;
  </script> 

But obviously, with this I would have to make one script for each element where I want to use this, and think of a new id every time, and change the hard-coded argument in the copy-pasted scripts individually. 但是显然,有了这个,我将不得不为每个要使用此元素的元素制作一个脚本,并每次都想一个新的id,并分别更改复制粘贴脚本中的硬编码参数。 Which is crazy. 太疯狂了

So, I thought about converting this into a function, and calling it for each element, but I don't know how. 因此,我考虑过将其转换为函数,并为每个元素调用它,但是我不知道该怎么做。 Or if that is even possible. 或者,即使有可能。 All examples I found where for clicking like this: 我找到的所有示例都可以像这样单击:

 <a href=javascript:function()>Click</a>

Which is not at all what I want. 这根本不是我想要的。

What I am looking for is a way to have something like this: 我正在寻找的是一种具有这样的方式:

 <selfdefinedtag arguments=(2014,0,23)></selfdefinedtag>

which is then processed by the script above, only that i from the script is not hard-coded, but initialised by the values in arguments . 然后由上面的脚本处理,只是脚本中的i不是硬编码的,而是由arguments的值初始化的。 And the colour and text is supposed to be different for each element. 并且每个元素的颜色和文本应该是不同的。

So my questions are: 所以我的问题是:

How can I convert i into an actual argument? 我如何转换i到实际参数? (I am especially unsure about this Date() construct.) (我尤其不确定此Date()构造。)

How can I apply my script to more than one element with different arguments? 如何将我的脚本应用于具有不同参数的多个元素?

If you have general criticisms of my code about best practices and the like, I would be happy to hear them. 如果您对我的代码中有关最佳做法等的一般性批评,我将很高兴听到它们。 I am not emotionally attached to what I have produced so far, so rip it apart all you like. 到目前为止,我对自己的作品并没有情感上的依恋,所以请撕碎它。

Just make a function out of your code... 只是用您的代码制作一个函数...

function changeElem(y1,m1,d1,elem){
 d = new Date();
   i = new Date(y1,m1,d1,0,0,0); //This is my "argument"
   var timesince = Math.floor((d.getTime() - i.getTime())/1000/60);
   var unit = "min";
   var colour = '#AAA';
   if (timesince > 60){
     timesince = Math.floor(timesince/60);
     unit = "h";
     if (timesince > 7) colour = '#a46744';
   }
   elem.innerHTML = timesince.toString()+unit;
   elem.style.color = colour;
}

To apply it to all a elements just do... 它适用于所有的a元素只是做...

var elem = document.getElementsByTagName("a");

[].forEach.call(elem, function(e){
   var y = +e.getAttribute("data-y");
   var m = +e.getAttribute("data-m");
   var d = +e.getAttribute("data-d");
   changeElem(y,m,d,e);
});

(I used + unary operator to convert the string to numberical value, getAttribute will fetch the data attributes.) (我使用+一元运算符将字符串转换为数字值, getAttribute将获取数据属性。)

And the a tag should be like a标签应该是这样的

<a href="..." data-y="2014" data-m="0" data-d="19">...</a>

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

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