像往常一样,单击选择元素时会显示选项元素,我想获取选项元素的高度。
谢谢。
编辑@david old school 选择
<select>
<option>a</option>
<option>s</option>
</select>
像往常一样,单击选择元素时会显示选项元素,我想获取选项元素的高度。
谢谢。
编辑@david old school 选择
<select>
<option>a</option>
<option>s</option>
</select>
这是不可能的,因为它实际上不是显示的选项元素。
HTML 标准仅指定浏览器应提供某种方式从选项中进行选择,而不是应如何显示。因此,没有标准方法可以获取有关其显示方式的任何信息。
常规浏览器将选项显示为某种下拉列表,但其他浏览器可能会以完全不同的方式显示它。例如,某些手机浏览器会显示一个覆盖整个屏幕的弹出窗口。
我相信这就是你想要的
$(document).ready(function () {
$("#mySelect").change(function () {
alert($(this.options[this.selectedIndex]).height());
});
});
如果你使用像 jquery 这样的库,你可以这样做:
$('#select option:first').height()
见这里。
如果您不使用 jquery,请查看offsetHeight。这应该可以得到你想要的。
在选择时,您可以这样做:
$(document).ready(function() {
$('#select').click(function() {
var H = $(this).children().height();
alert(H);
});
});