具有特定类名的所有元素的Javascript数组 [英] Javascript array from all elements with a certain class name

查看:96
本文介绍了具有特定类名的所有元素的Javascript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的网页中某个类的元素创建一个数组。数组应该使用类videoLink从所有标签中获取videofile属性值。

I am trying to make an array from elements with a certain class in my web page. The array should get the videofile attribute value from all a tags with the class videoLink.

数组中的最终值应为。

 cycling_large, ocean_medium, winecountry_part1



 <a class="videoLink"  videofile="cycling_large"  ></a>
 <a class="videoLink" videofile="ocean_medium" ></a>
 <a class="videoLink" videofile="winecountry_part1" ></a>

我试过这个,但是不行。

I tried this but, does not work.

var values = $('.videoLink').map(function() { return this.attr('videofile'); }).get();

提前致谢。

推荐答案

var links = document.getElementsByClassName("videoLink");
var values = [].map.call(links, function (el) {
  return el.getAttribute("videofile");
});

因为你没有简单的jQuery。

Because you don't jQuery for simple things.

浏览器支持:

  • ES5 shim
  • DOM shim

这篇关于具有特定类名的所有元素的Javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆