我有一个非常奇怪的问题
我正在加载两个单独的div,如下所示,span元素是为了存储每个Isotope实例中每行有多少项
<div class='instagram' id='insta12'>
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
<span id="hiddenValue" >5</span>
</div>
<div class='instagram' id='insta101'>
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
<span id="hiddenValue">3</span>
</div>
我的js文件如下所示
jQuery(document).ready(function($) {
var tmpNum="";
var numberOfImages=3;
var $container = $('.instagram');
$container.each(function () {
tmpNum= $(this).find('span#hiddenValue').text();
numberOfImages=parseInt(tmpNum);
var tmpid=$(this).attr("id");
alert(tmpid);
$("#"+tmpid).imagesLoaded(function() {
$("#"+tmpid).isotope({
resizable: false,
// disable normal resizing
// set columnWidth to a percentage of container width
masonry: {
columnWidth: $("#"+tmpid).width() / numberOfImages
}
});
}); //isotope
}); //each
}); //dom ready
虽然脚本似乎正在运行,但第二个容器每行的项目数与第一个容器一样多。有什么想法吗?
答案 0 :(得分:0)
<强>解决强>
jQuery(document).ready(function($) {
var tmpNum = "";
var $container = $('.instagram');
$container.each(function() {
var tmpid = $(this).attr("id");
tmpNum = $("#" + tmpid + " span#hiddenValue").text();
var numberOfImages = parseInt(tmpNum, 10);
$(this).imagesLoaded(function() {
$(this).isotope({
resizable: false,
// disable normal resizing
// set columnWidth to a percentage of container width
masonry: {
columnWidth: $(this).width() / numberOfImages
}
});
}); //isotope
}); //each
}); //dom ready