jquery - 通过 jquery 函数传递元素

标签 jquery

我想遍历每个 .clipped-box 元素并将它们传递给以下函数:

(genClips = function() {        
    // For easy use
    $t = $('.clipped-box');     
    // Like I said, we're using 5!
    var amount = 5;     
    // Get the width of each clipped rectangle.
    var width = $t.width() / amount;
    var height = $t.height() / amount;      
    // The total is the square of the amount
    var totalSquares = Math.pow(amount, 2);     
    // The HTML of the content
    var html = $t.find('.content').html();  
    var y = 0;

    for(var z = 0; z <= (amount*width); z = z+width) { 

        $('<div class="clipped" style="clip: rect('+y+'px, '+(z+width)+'px, '+(y+height)+'px, '+z+'px)">'+html+'</div>').appendTo($t);

        if(z === (amount*width)-width) {

            y = y + height;
            z = -width;

        }

        if(y === (amount*height)) {
            z = 9999999;
        }

    }

})();

我在 HTML 中有几个 .clipped-box 元素,如下所示:

<div class="clipped-box piece1">
        <div class="content">
            <img src="img/piece1.png">
        </div>      
    </div>

    <div class="clipped-box piece2">
        <div class="content">
            <img src="img/piece2.png">
        </div>      
    </div>

我该如何使其适应这种情况?

最佳答案

你可以这样做:

$('.clipped-box').each(function(){
    var $box = $(this);
    // implement everything here for this single box.
});

这样,您就可以将多个框全部包含在各自的范围内

关于jquery - 通过 jquery 函数传递元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25267811/

相关文章:

php - LimeSurvey RemoteControl2 API - 有 add_response PHP 示例吗?

javascript - 在 JQuery 中链接到带有空格的文件

JQuery .replaceWith() 或 .html()

javascript - e.parentNode.parentNode.childNodes[i].innerText 的 Jquery 等效/浏览器兼容性;

jquery - 如何切换 "a"标签元素以显示隐藏的 DIV,就像 Facebook 通知中心一样

Javascript 在单元格仅包含 0 后停止执行;

javascript - jQuery Mobile ListView 样式在清空并动态重新创建列表项后不刷新

javascript - 检查页面是否在某个部分

javascript - jQuery 中的单击事件调用两次或多次

javascript - Jquery 第一个单词大写字母