简体   繁体   English

jQuery的选择框-我怎么能有两个输入不同的宽度

[英]jquery select-box - how can I have two inputs different widths

I'm using this jquery plugin to make my select dropdown boxes look nicer. 我正在使用此jquery插件使我的选择下拉框看起来更好。 http://code.google.com/p/select-box/ http://code.google.com/p/select-box/

Here's a fiddle of it working: http://jsfiddle.net/FQKax/1/ 这是一个有效的工具: http : //jsfiddle.net/FQKax/1/

I want to have the two dropdowns to be different widths but I've tried wrapping them in divs, tried to hack the js to give them different ids, everything I can think of but no joy. 我想让两个下拉列表具有不同的宽度,但是我尝试将它们包装在div中,试图破解js以给它们提供不同的id,我能想到的一切都没有喜悦。

Also I'm ashamed to admit I can't seem to change the color of the text in the actual dropdown bit. 我也很ham愧地承认我似乎无法更改实际下拉位中文本的颜色。 I can change the backgound colour etc but buggered if I can change the color of the text... weird 我可以更改背景色等,但是如果可以更改文本的颜色会感到困惑……很奇怪

There's an option that you can specify what to use as classname for the sbHolder object, but you don't want to change that since you would need to rewrite the CSS. 有一个选项,您可以指定用作sbHolder对象的类名的sbHolder ,但是您不想更改它,因为您需要重写CSS。 It'd be nice if they let you set an additional class to apply, but they don't. 如果他们让您设置一个附加的类来应用,那很好,但他们没有。

I would just put a wrapper around the select element and use CSS to override the default width http://jsfiddle.net/FQKax/8/ 我只是将包裹器包裹在select元素上,并使用CSS覆盖默认宽度http://jsfiddle.net/FQKax/8/

.wrapper-one .sbHolder{
    width: 500px;
}

.wrapper-two .sbHolder {
    width: 200px;
}


<div class="wrapper-one">
<select id="language">
    <option value="javascript">Javascript</option>
    <option value="objective-c">Objective C</option>
    <option value="python">Python</option>
</select>
</div>
<br/><br/>

<div class="wrapper-two">
<select id="language2">
    <option value="javascript">Javascript</option>
    <option value="objective-c">Objective C</option>
    <option value="python">Python</option>
</select>
</div>

This requires adding some markup, @cih's answer doesn't. 这需要添加一些标记,@cih的答案不需要。 It just requires using jQuery to mark each instance accordingly http://jsfiddle.net/FQKax/37/ 它只需要使用jQuery相应地标记每个实例http://jsfiddle.net/FQKax/37/

$("#language").selectbox();
$("#language2").selectbox();

$(".sbHolder").each(function(index){
    $(this).addClass('instance-' + index);
});

.instance-0.sbHolder{
    width: 500px;
}

.instance-1.sbHolder {
    width: 200px;
}
$(".sbHolder").first().addClass("first");

That will add a class you can target on you first checkbox, there better way to iterate through multiple selectors, check out this link.. 这将添加一个您可以在您的第一个复选框上定位的类,那里是更好的方法来遍历多个选择器,请查看此链接。

Other than that Joe answers the rest of your question. 除此之外,乔回答了其余的问题。

Try this http://jsfiddle.net/FQKax/30/ 试试这个http://jsfiddle.net/FQKax/30/

<link href="http://select-box.googlecode.com/svn/tags/0.2/jquery.selectbox.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript" src="http://select-box.googlecode.com/svn/tags/0.2/jquery.selectbox-0.2.min.js"></script>


<select id="language">
 <option value="javascript">Javascript</option>
 <option value="objective-c">Objective C</option>
 <option value="python">Python</option>
</select>
<br/><br/>

<select id="language2">
  <option value="javascript">Javascript</option>
  <option value="objective-c">Objective C</option>
  <option value="python">Python</option>
 </select>


  ##### JQUERY #######
      $(function () {
        $("#language").selectbox();
        $("#language2").selectbox();
        $(".sbHolder").each(function(){
          var $langDom = $(this);
          if($langDom.prev().attr('id') == 'language'){
            $langDom.addClass("language_1");  
          } else if($langDom.prev().attr('id') == 'language2') {
            $langDom.addClass("language_2");          
         }
       });
     });

    ###### CSSS TO ADD #####
   .language_1{
     width: 1200px;
    }

    .language_2{
      width: 200px;
     }

For the text color, change .sbOptions a:link, .sbOptions a:visited {} and/or .sbOptions a:hover, .sbOptions a:focus, .sbOptions a.sbFocus {} . 对于文本颜色,更改.sbOptions a:link, .sbOptions a:visited {}和/或.sbOptions a:hover, .sbOptions a:focus, .sbOptions a.sbFocus {}

For the widths, .sbOptions is your dropdown width, and .sbHolder {} is the width of the "currently selected" item. 对于宽度, .sbOptions是您的下拉宽度, .sbHolder {}是“当前选定”项目的宽度。

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

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