简体   繁体   English

结合jQuery / JavaScript函数

[英]Combining jQuery/JavaScript functions

Is there any way to combine all of this to reduce the amount of javascript? 有没有办法结合所有这些来减少javascript的数量?

    $(document).ready(function() {

    $(".individualImagebox img").bind("click", function()
    {
        var src = $(this).attr("src");

        if (src.search(/Red\.jpg$/) >= 0) return;

        // Removes the red overlay from the images folder
        $('.individualImagebox img').attr( "src", function () {  
            var thisSRC = $(this).attr( "src");
            return thisSRC.replace(/Red\.jpg$/, ".jpg");
        });

        // Adds the red overlay from the images folder
        $(this).attr( "src", src.replace(/\.jpg$/, "Red.jpg")  );
    });

});

function ShowHide(index) {

    var itemSelector = ".name:eq(" + index + ")";

    $(".name .bio").fadeOut();
    $(".name").not(itemSelector).fadeOut();

    $(itemSelector).animate({"height": "show"}, { duration: 500 });
    $(itemSelector + " .bio").animate({"height": "show"}, { duration: 500 });

}

Combining methods won't save you space. 组合方法不会为您节省空间。 Take a look at 看一眼

http://developer.yahoo.com/yui/compressor/ http://developer.yahoo.com/yui/compressor/

$(function() { // $(function(){}) is a shortcut of $(document).ready(function(){})

    var $activeImg; // Maintain a reference to the last activated img

    $(".individualImagebox img").click(function(){
        if (!!$activeImg) {
            $activeImg.attr("src", function(i, src){
                return src.replace(/(.+)Red\.jpg$/, "$1.jpg");
            });
        }
        $activeImg = $(this).attr("src", function(i, src){ // replace attribute and updates active img reference
            return src.replace(/(.+)\.jpg$/, "$1Red.jpg");
        });
    });
});

I don't know exactly what you are trying to do but if possible, you should toggling a class instead of modifying the src attribute. 我不确切地知道你要做什么,但如果可能的话,你应该切换一个类而不是修改src属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM