jquery ui selectmenu:如何正确设置动态宽度?

发布于 2025-01-03 21:15:08 字数 244 浏览 1 评论 0原文

我想使用来自 felixnagel 的 jquery ui selectmenu。它有效,但我有一个问题,jquery 总是为选择菜单设置太小的宽度,以便打开/关闭图标出现在内容上方。我知道我可以在命令 selectmenu() 中设置宽度,但我有几个不同的选择菜单,所以我想知道我可以在哪里影响正确计算选择菜单的宽度,以便我可以给出它是一个额外的 px 宽度。

我试图在 .js 文件中找到它,但从现在起我没有成功找到它。希望你们中有人知道我在这里能做什么。

i would like to use jquery ui selectmenu from felixnagel. it works but i have the problem that jquery always set a too small width for the selectmenu so that the open/close icon appears above the content. I know that i can set the width in the command selectmenu() but i have a couple of different selectmenus, so i would like to know where i can take influence on the correct calculation how wide the selectmenu will be, so that i can give it an additional px for the width.

I tried to find it in the .js files, but since now i was not successfull with it. Hopefully someone of you have an idea what i can do here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千仐 2025-01-10 21:15:08

这很简单,并且可以根据内容进行调整。

$( "select" ).selectmenu({ width : 'auto'});

这使用硬编码宽度来避免选择评论中提到的不同宽度的项目时出现的问题:

$( "select" ).selectmenu({ width : 250});

This is simple and makes it adjust based on content.

$( "select" ).selectmenu({ width : 'auto'});

This uses a hard coded width to avoid the problem when selecting items of different widths mentioned in the comment:

$( "select" ).selectmenu({ width : 250});
苍景流年 2025-01-10 21:15:08

也许这样的东西会帮助你

<select width="200">
    <option>Small</option>
</select>


$(document).ready(function(){
    $.each($('select'), function () {
        $(this).selectmenu({ width : $(this).attr("width")})
    })
})

你遍历每个选择元素,你将有可能在选择元素宽度属性中指定 with 。也许您发现它很有用并且可以对其进行修改以满足您的需求。

编辑1:
或者,如果你只是想通过代码添加一些额外的东西(我认为你也可以使用 CSS 来做到这一点),你可以使用类似的东西:

<select>
    <option>Small</option>
</select>


$(document).ready(function(){
    $.each($('select'), function () {
        $(this).selectmenu({ width : $(this).width() + 100}) // You just take the width of the current select and add some extra value to it
    })
})

Maybe something like this will help you

<select width="200">
    <option>Small</option>
</select>


$(document).ready(function(){
    $.each($('select'), function () {
        $(this).selectmenu({ width : $(this).attr("width")})
    })
})

You walk thru every select element and you will have the possibilty to specify the with in the select elements width attribute. Maybe you find it useful and can modify it so it fill your needs.

Edit 1:
Or if you just want to add something extra thru code (I think you can do it also with CSS) you could use something like:

<select>
    <option>Small</option>
</select>


$(document).ready(function(){
    $.each($('select'), function () {
        $(this).selectmenu({ width : $(this).width() + 100}) // You just take the width of the current select and add some extra value to it
    })
})
蛮可爱 2025-01-10 21:15:08

这是一种黑客,我不知道它有多强大,但我已经在一个特定的实例中成功地使用了它:

$("select").each(function(){
    var $this=$(this);
    $this.selectmenu();
    var menu=$this.selectmenu("menuWidget");
    var button=$this.selectmenu("widget");
    button.click();
    var longest=null;
    menu.children("li").each(function(){
        var $this=$(this);
        if(longest==null || longest.text().length < $this.text().length){
            longest=$this;
        }
    });
    menu.css("width","auto");
    var width=longest.mouseover().width();
    button.click();
    var innerButton = button.find(".ui-selectmenu-text");
    var paddingRight=parseFloat(innerButton.css("padding-right"));
    var paddingLeft=parseFloat(innerButton.css("padding-left"));
    var total=width+paddingLeft+paddingRight + 2;
    $this.selectmenu("option","width", total);
})

简而言之,它的作用是:

  • 打开菜单,以便填充并布局
  • 找到其最长的元素
  • 将菜单宽度设置为自动,将鼠标悬停在最长的元素上并获取其宽度
  • 关闭菜单
  • 获取按钮小部件的左右填充
  • 将其添加到最长项目的计算宽度并将其用作宽度
    • 添加 2 个像素以达到更好的效果(我认为是边框之类的)

,只是它在很大程度上依赖于 jquery-ui 内部的处理方式,因此它可能会在任何更新时中断。

请参阅此小提琴:http://jsfiddle.net/txo1mvhn/2/

It's kind of a hack and I don't know how robust this is but I've successfully used this in one particular instance:

$("select").each(function(){
    var $this=$(this);
    $this.selectmenu();
    var menu=$this.selectmenu("menuWidget");
    var button=$this.selectmenu("widget");
    button.click();
    var longest=null;
    menu.children("li").each(function(){
        var $this=$(this);
        if(longest==null || longest.text().length < $this.text().length){
            longest=$this;
        }
    });
    menu.css("width","auto");
    var width=longest.mouseover().width();
    button.click();
    var innerButton = button.find(".ui-selectmenu-text");
    var paddingRight=parseFloat(innerButton.css("padding-right"));
    var paddingLeft=parseFloat(innerButton.css("padding-left"));
    var total=width+paddingLeft+paddingRight + 2;
    $this.selectmenu("option","width", total);
})

In a nutshell what it does is:

  • open the menu so that it is filled and laid out
  • find its longest element
  • set the menu width to auto, hover over the longest element and get its width
  • close the menu
  • get the left and right padding of the button widget
  • add that to the computed width of the longest item and use that as the width
    • adding 2 pixels for good measure (I think it's borders or something)

It's just that it kind of heavily relies on how jquery-ui does things internally so it might break upon any update.

See this fiddle: http://jsfiddle.net/txo1mvhn/2/

不爱素颜 2025-01-10 21:15:08

以前的解决方案几乎是正确的。

您必须设置 css 宽度并调用 selectmenu ,如下所示:

$('select').selectmenu({ width : $(this).css('width')});

Previous solutions were almost right.

You have to set css width and call selectmenu like this:

$('select').selectmenu({ width : $(this).css('width')});
冷︶言冷语的世界 2025-01-10 21:15:08

对于百分比,如下所示:

在 CSS 中声明百分比:

 select{
    width: 100%;
 }

在 JavaScript 中声明:

jQuery("select").selectmenu(
        {
            width:  jQuery(this).attr("width"),
        }   
);

For percentages, is like this:

Declare the percentage in your CSS:

 select{
    width: 100%;
 }

And in your JavaScript:

jQuery("select").selectmenu(
        {
            width:  jQuery(this).attr("width"),
        }   
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文