JavaScript – JQuery选择框和循环帮助

前端之家收集整理的这篇文章主要介绍了JavaScript – JQuery选择框和循环帮助前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
谢谢阅读.我有点新的jQuery,并且我试图制作一个脚本,我可以包括在我所有的网站,以解决一个总是让我疯狂的问题…

问题:
选择具有长选项的框在Internet Explorer中被切断.例如,这些选择框:
http://discoverfire.com/test/select.php

在Firefox中,它们很好,但是在IE中,当它们下拉时,选项将被切断到选择的宽度.

解决方案:
我想要做的是创建一个脚本,我可以在任何可以包含以下内容页面添加

>循环浏览页面上的所有选择.
>对于每个选择:
循环选择.
B.找到最长选项的宽度.
C.绑定一个函数以将焦点扩展到该宽度(或者可能点击…).
D.绑定功能缩小到原始宽度的模糊.

我已经设法做了大部分步骤#2的一个选择框.

我发现获取选项宽度是一个问题(特别是在IE中),所以我循环遍历并将每个选项的文本复制到一个跨度,测量跨度宽度,并使用最长的一个作为选择将被扩展到的宽度.也许有人有一个更好的想法.

这是代码

  1. <script type='text/javascript'>
  2.  
  3. $(function() {
  4.  
  5. /*
  6. This function will:
  7. 1. Create a data store for the select called ResizeToWidth.
  8. 2. Populate it with the width of the longest option,as approximated by span width.
  9.  
  10. The data store can then be used
  11. */
  12. // Make a temporary span to hold the text of the options.
  13. $('body').append("<span id='CurrentOptWidth'></span>");
  14.  
  15. $("#TheSelect option").each(function(i){
  16.  
  17. // If this is the first time through,zero out ResizeToWidth (or it will end up NaN).
  18. if ( isNaN( $(this).parent().data('ResizeToWidth') ) ) {
  19. $(this).parent().data( 'OriginalWidth',$(this).parent().width() );
  20. $(this).parent().data('ResizeToWidth',0);
  21.  
  22. $('CurrentOptWidth').css('font-family',$(this).css('font-family') );
  23. $('CurrentOptWidth').css('font-size',$(this).css('font-size') );
  24. $('CurrentOptWidth').css('font-weight',$(this).css('font-weight') );
  25.  
  26. }
  27.  
  28. // Put the text of the current option into the span.
  29. $('#CurrentOptWidth').text( $(this).text() );
  30.  
  31. // Set ResizeToWidth to the longer of a) the current opt width,or b) itself.
  32. //So it will hold the width of the longest option when we are done
  33. ResizeToWidth = Math.max( $('#CurrentOptWidth').width(),$(this).parent().data('ResizeToWidth') );
  34.  
  35. // Update parent ResizeToWidth data.
  36. $(this).parent().data('ResizeToWidth',ResizeToWidth)
  37.  
  38. });
  39.  
  40. // Remove the temporary span.
  41. $('#CurrentOptWidth').remove();
  42.  
  43. $('#TheSelect').focus(function(){
  44. $(this).width( $(this).data('ResizeToWidth') );
  45. });
  46.  
  47. $('#TheSelect').blur(function(){
  48. $(this).width( $(this).data('OriginalWidth') );
  49. });
  50.  
  51.  
  52. alert( $('#TheSelect').data('OriginalWidth') );
  53. alert( $('#TheSelect').data('ResizeToWidth') );
  54.  
  55. });
  56.  
  57.  
  58. </script>

和选择:

  1. <select id='TheSelect' style='width:50px;'>
  2. <option value='1'>One</option>
  3. <option value='2'>Two</option>
  4. <option value='3'>Three</option>
  5. <option value='42,693,748,756'>Forty-two billion,six-hundred and ninety-three million,seven-hundred-forty-some-odd..... </option>
  6. <option value='5'>Five</option>
  7. <option value='6'>Six</option>
  8. <option value='7'>Seven...</option>
  9. </select>

希望如果你想运行它,你可以运行它,或者你可以在这里看到它:http://discoverfire.com/test/select.php.

我需要什么帮助:
这需要一点抛光,但如果您指定选择框,似乎可以正常工作.

但是,我似乎无法弄清楚如何将它应用到带有循环的页面上的所有选择框中.到目前为止,我有这个:

  1. $('select').each(
  2. function(i,select){
  3. // Get the options for the select here... can I use select.each...?
  4. }
  5. );

另外,有没有更好的方法来获得每个选择的最长选项的长度?跨度接近,但不是非常精确.问题是IE为实际选择的选项宽度返回零.

任何想法都是非常受欢迎的,无论是问题,还有任何其他改进我的代码.

谢谢!!

解决方法

修改每个选择,请尝试:
  1. $('select').each(function(){
  2.  
  3. $('option',this).each(function() {
  4. // your normalizing script here
  5.  
  6. })
  7.  
  8. });

第二个参数(this)在第二个jQuery调用范围内选择器(‘option’),所以它本质上就是这个select中的所有选项元素.如果没有提供,您可以将第二个参数默认为’document’.

猜你在找的jQuery相关文章