0
我有2个div具有相同的类,但它们具有不同的值。 我想在jQuery循环中使用不同的值,但由于某种原因,脚本只使用第一个div的值。未能从同一类的2个div获得不同的值
这里是demo fiddle
HTML:
<div class="temp-stars"><p>5</p></div>
<div class="temp-stars"><p>2</p></div>
CSS:
.temp-stars { position: relative; background: #666; padding: 10px; width: 100px; height: 10px; }
.temp-stars p {display: none;}
.stars {position: relative; width: 100%; height: 100%; }
.single-star-yellow { width: 10px; height: 10px; background: #f90; float: left; margin-right:5px; }
的jQuery:
$(function() {
var num = parseInt($(".temp-stars p").html());
alert(num)
$(".temp-stars").append("<div class='stars'></div>")
for (var i = 0; i < num; i++) {
$(".stars").append("<div class='single-star-yellow'></div>")
};
});
谢谢。这工作非常好。我确实尝试过使用每个函数,但是查看你的代码我知道我有很多东西在我的尝试中失踪。 –