variable in nth-child() jqueryjavascript - Stack Overflow

i have the following code:$(document).ready(function(){var max_count = $(".slider").children(

i have the following code:

$(document).ready(function(){
    var max_count = $(".slider").children().length;
    function fn_get_natural_dim(slide_image,img){
        var width = img.width;
        var height = img.height;
        var ratiow = width/600;
        var ratioh = height/400;

        if(ratiow>=ratioh)
            {
            height = height/ratiow;
            $(slide_image).css("width","600px");
            $(slide_image).css("height",height);
            var margin = (400-height)/2;
            $(slide_image).css("margin-top",margin);
            }
        else
            {
            width = width/ratioh;
            $(slide_image).css("width",width);
            $(slide_image).css("height","400px");
            var margin = (600-width)/2;
            $(slide_image).css("margin-left",margin);
            }
    }
    for(var count=1;count<=max_count;count++)
        {
        var count_string = count.toString();
        var img_name = "img" + count_string;
        var slide_image = $('.slider > li:nth-child(" + count + ")');

        var img = new Image();
        img.onload = (function(slide_image,img){
            return function() {fn_get_natural_dim(slide_image,img);};
        })(slide_image,img);
        img.src = $(slide_image).attr("src");
        }
});

i want the variable slide_image to select the nth list, where n should be the same as the variable count. i added the + signs (although i dont know why i have to) and made the quote symbols different, and yet it still doesnt work

i have the following code:

$(document).ready(function(){
    var max_count = $(".slider").children().length;
    function fn_get_natural_dim(slide_image,img){
        var width = img.width;
        var height = img.height;
        var ratiow = width/600;
        var ratioh = height/400;

        if(ratiow>=ratioh)
            {
            height = height/ratiow;
            $(slide_image).css("width","600px");
            $(slide_image).css("height",height);
            var margin = (400-height)/2;
            $(slide_image).css("margin-top",margin);
            }
        else
            {
            width = width/ratioh;
            $(slide_image).css("width",width);
            $(slide_image).css("height","400px");
            var margin = (600-width)/2;
            $(slide_image).css("margin-left",margin);
            }
    }
    for(var count=1;count<=max_count;count++)
        {
        var count_string = count.toString();
        var img_name = "img" + count_string;
        var slide_image = $('.slider > li:nth-child(" + count + ")');

        var img = new Image();
        img.onload = (function(slide_image,img){
            return function() {fn_get_natural_dim(slide_image,img);};
        })(slide_image,img);
        img.src = $(slide_image).attr("src");
        }
});

i want the variable slide_image to select the nth list, where n should be the same as the variable count. i added the + signs (although i dont know why i have to) and made the quote symbols different, and yet it still doesnt work

Share Improve this question edited Dec 1, 2016 at 13:51 Sibtain 10817 bronze badges asked Jul 24, 2013 at 16:37 quasicoherent_drunkquasicoherent_drunk 1311 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You should do it like this:

$('.slider > li:nth-child(' + count + ')');

The problem is that you're not closing the strings/segments surrounding the variable, so:

'.slider > li:nth-child(" + count + ")' 

Is just a long string, a selector which makes no sense; that's why it matches nothing, you have to actually separate the sections by ending the static parts with the same kind of quote...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745313980a4622130.html

相关推荐

  • variable in nth-child() jqueryjavascript - Stack Overflow

    i have the following code:$(document).ready(function(){var max_count = $(".slider").children(

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信