Getting the height of an option element with javascript?

后端 未结 4 866
梦谈多话
梦谈多话 2020-12-12 07:55

As usual, option elements are shown when a select element is clicked and I\'d like to get the height of the option elements.

Thanks.

Edit @d

相关标签:
4条回答
  • 2020-12-12 08:05

    That's not possible, as it's actually not the option elements that are shown.

    The HTML standard only specifies that the browser should provide some way of choosing from the options, not how that should be displayed. Therefore there is no standard way of getting any information about how it's displayed.

    Regular browsers show the options as some kind of dropdown list, but other browsers may show it in a completely different way. Some mobile phone browsers for example shows a popup that covers the entire screen.

    0 讨论(0)
  • 2020-12-12 08:08

    If you're using a library like jquery, you can do:

    $('#select option:first').height()

    See here.

    If you're not using jquery, look at offsetHeight. This should get you what you want.

    0 讨论(0)
  • 2020-12-12 08:10

    I believe this is what you want

    $(document).ready(function () {
      $("#mySelect").change(function () {
        alert($(this.options[this.selectedIndex]).height());
      });
    });
    

    Here is a demo http://jsfiddle.net/xf3wD/

    0 讨论(0)
  • 2020-12-12 08:19

    On select you can do that:

    $(document).ready(function() {
    
        $('#select').click(function() {
            var H = $(this).children().height();
            alert(H);
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题