<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<p id="fl">
<img src="/images/flags/il.svg" data-cc="a" class="zastavica" />
<img src="/images/flags/il.svg" data-cc="b" class="zastavica" />
<img src="/images/flags/il.svg" data-cc="c" class="zastavica" />
</p>
<script>
var collection = $(this).attr('data-cc');
console.log(collection);
</script>
</body>
</html>
答案 0 :(得分:3)
使用.each()
循环抛出图像并将值添加到数组
请参阅代码段
var collection = [];
$('[data-cc]').each(function() {
collection.push($(this).attr('data-cc'));
});
console.log(collection);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="fl">
<img src="/images/flags/il.svg" data-cc="a" class="zastavica" />
<img src="/images/flags/il.svg" data-cc="b" class="zastavica" />
<img src="/images/flags/il.svg" data-cc="c" class="zastavica" />
</p>
&#13;