2

我有以下代码用于初始化 wookmark

$container.find('.item').wookmark({
            itemWidth: 200,
            offset: 38,
            autoResize: true,
            container: $container
        });

但我想为每个元素制作不同的宽度取决于元素中的属性,我尝试了类似的东西

...
itemWidth: $(this).attr('imageWidth') ? parseInt($(this).attr(imageWidth)) : 200,
...

但这并不指向“.item”元素,它仍然指向$container

4

1 回答 1

0

It should be so.

jQuery.each() iterator and assign different itemWidth to each element like this:

$('.item').each(function(el) {
  $(el).wookmark({
    itemWidth: $(el).attr('imageWidth')
  });
});

Why this doesn't refer to item but to $container? Because you call .find() on $container then jQuery returns still $container and you call .wookmark on it.

this refers to caller object.

于 2013-07-01T09:13:53.987 回答