0

简化的 JSfiddle 在这里:http: //jsfiddle.net/GGUq8/

我有一个小脚本,它需要li's 并将它们分成 div。如果我将脚本设置为 3,li则每个 div 最多包含 3 个。

它工作正常,但是当我对同一页面上的两个元素使用相同的类时会出现问题。当我这样做时,划分出错了,我最终得到了两个具有相同内容的元素......

请检查jsfiddle以获取示例。

4

1 回答 1

0

You could leverage .each() for this:

container.each(function( Index ) {

Basically when you set container to $(containerClass) it is finding all elements on the page and working on both of them. So when you call the function once, it works on the first element it finds, but updates all of the elements on the page with that class. Then it works on the next one it finds, and updates all of them again.

By using each(), you'll loop over all of them individually and update each of them individually. To accomplish this, you'll want to change everywhere you were using container with a var set to $(this).

I've made an update to your script. I think it is working as you are expecting.

于 2013-01-22T06:54:33.437 回答