简体   繁体   English

如何将jQuery选择传递给函数?

[英]How to pass jquery selections into a function?

I tried searching, but couldn't quite find what I was looking for. 我尝试搜索,但找不到我想要的东西。 Hopefully this is super simple for someone. 希望这对某人来说超级简单。

This is the code i'm starting with that i'd like to turn into a function. 这是我要转换为功能的代码。 Obviously there is a way to do this so I don't have to repeat. 显然,有一种方法可以做到这一点,因此我不必重复。

var fullWidth = $('.full-img').width();
var paddBottom = fullWidth * .75;
$('.full-img').css('padding-bottom', paddBottom);

var thumbWidth = $('.img-box').width();
var thumbBottom = thumbWidth * .75;
$('.img-box').css('padding-bottom', thumbBottom);

Here's what I tried 这是我尝试过的

function aspectRatioFix(main, thumb) {
    var fullWidth = main.width();
    var paddBottom = fullWidth * .75;
    main.css('padding-bottom', paddBottom);

    var thumbWidth = thumb.width();
    var thumbBottom = thumbWidth * .75;
    thumb.css('padding-bottom', thumbBottom);
}

    aspectRatioFix('.full-img', '.img-box');

I get the error "Uncaught TypeError: main.width is not a function" 我收到错误“未捕获的TypeError:main.width不是函数”

Is there a way to maybe loop through my function to take care of both parts of my script instead of just duplicating what I have? 有没有一种方法可以遍历我的函数来处理脚本的两个部分,而不仅仅是复制我的脚本? Thanks! 谢谢!

The function aspectRatioFix is expecting jQuery objects so you'll have to pass in jQuery objects instead of selectors (strings) like this: 函数aspectRatioFix需要使用jQuery对象,因此您必须传递jQuery对象而不是像这样的选择器(字符串):

aspectRatioFix($('.full-img'), $('.img-box'));

If you have many images and their thumbs and you want to apply the code for all of them then use .each like this: 如果您有很多图像和它们的拇指,并且想要为所有图像应用代码,则使用.each如下所示:

var $mains = $('.full-img'),
    $thumbs = $('.img-box');

$mains.each(function(index) {
    // this:  is the main image
    // index: is its index so we can access the according thumb from $thumbs

    aspectRatioFix($(this), $($thumbs[index])); // apply the aspectRatioFix on this main image, and on it's thumb from the $thumbs object (both this and $thumbs[index] are node elements so we have to wrap them in a jQuery object using $())
});

You are sending plain strings and not selectors to your function, you would have to do it like this: 您正在向函数发送纯字符串而不是选择器,您必须这样做:

function aspectRatioFix(main, thumb) {
    var fullWidth = main.width();
    var paddBottom = fullWidth * .75;
    main.css('padding-bottom', paddBottom);

    var thumbWidth = thumb.width();
    var thumbBottom = thumbWidth * .75;
    thumb.css('padding-bottom', thumbBottom);
}

aspectRatioFix($('.full-img'), $('.img-box'));

What I can suggest is simply caching your jQuery objects. 我可以建议的只是缓存您的jQuery对象。 A simple DOM ready event inside an IIFE which can be copy/pasted into a separate js file: IIFE中的一个简单的DOM ready事件,可以将其复制/粘贴到单独的js文件中:

// IIFE
(function(scope, $){
    // this configuration
    var cfg = {
        cache: {
            fullImg: '.full-img',
            imgBox: '.img-box'
        },
        options: {
            modifier: 0.75,
            css: 'padding-bottom'
        }
    };

    // function declaration
    function aspectRatioFix(elements, options){
        $.each(elements, function(){
           var el = $(this),
               paddBottom = el.width() * options.modifier;
           el.css(options.css, paddBottom);
        });
    }

    // DOM ready to execute
    $(function(){
        var fullImg = $(cfg.cache.fullImg);
        //var imgBox = $(cfg.cache.imgBox);

        aspectRatioFix(fullImg, cfg.options);
        aspectRatioFix($(cfg.cache.imgBox), cfg.options);
    });
}(window, window.jQuery));

The way I refactor things into functional (in this case) and modular approaches is to move every string in the code to cfg object literal. 我将事物重构为功能性(在这种情况下)和模块化方法的方式是将代码中的每个字符串移至cfg对象文字。 The code becomes more clear to separate. 该代码变得更加清晰可分离。

And to add real quickly: aspectRatioFix($('.a-new-selector'), cfg.options); 并快速添加真实内容: aspectRatioFix($('.a-new-selector'), cfg.options); In case of the accepted answer you'll have to edit the function again while you should avoid that. 如果答案被接受,则必须再次编辑该功能,而应避免该行为。

function aspectRatioFix(main, thumb) { 
    var fullWidth = main.width(); 
    var paddBottom = fullWidth * .75; 
    main.css('padding-bottom', paddBottom); 
    var thumbWidth = thumb.width(); 
    var thumbBottom = thumbWidth * .75;
    thumb.css('padding-bottom', thumbBottom);
 } 
 aspectRatioFix($('.full-img'), $('.img-box'));

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

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