根据它们的数量设置具有相同类别的多个元素的宽度 [英] Setting width of multiple elements with same class depending on their number

查看:138
本文介绍了根据它们的数量设置具有相同类别的多个元素的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到类似于我之前遇到的问题。我试图给多个元素的宽度取决于它们有多少。

I'm experiencing a problem similar with one I had before. I'm trying to give multiple elements a width depending on how many of them exist.

说我有5个元素,它们的宽度必须是20%,如果我有4 25%等(width = 100 / n)

Say I have 5 elements, their widths have to be 20%, if I have 4 25% etc. (width = 100 / n)

我认为这会工作,但不会:

I thought this would work, but it doesn't:

    if ($(window).width() <= 960) {

        $("aside.widget").css("width", function(){
            var widgetLength = $("section#secondary").children("aside.widget").length();
            return 100 / widgetLength + "%"
        });
    }

也不会:

    if ($(window).width() <= 960) {         
        $("aside.widget").css("width", 100 / widgetLength + "%");
    }

控制台返回: Uncaught TypeError: of object [object Object]不是函数

任何想法?感谢。

推荐答案

它们应该都很好,但长度需要改变:

They should both work just fine, but length is not a function, so you need to change this:

var widgetLength = $("section#secondary").children("aside.widget").length();

var widgetLength = $("section#secondary").children("aside.widget").length;

那么你的问题中的方法都可以工作!

then both the methods in your question will work!

这篇关于根据它们的数量设置具有相同类别的多个元素的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆