在 IE 中,如何找到
我有以下 HTML 代码:
<html>
<head>
<title>Cheeses</title>
</head>
<body>
<select name="multi" id="multi" multiple="multiple">
<option selected="selected" label="emmental">Emmental</option>
<option label="roquefort" >Roquefort</option>
<option label="parmigiano">Parmigiano</option>
<option label="cheddar">Cheddar</option>
</select>
</body>
</html>
在 IE 中的 JavaScript 中,如何获取 元素之一的尺寸和位置?执行以下操作不起作用:
var selectEl = document.getElementById("multi");
var options = selectEl.getElementsByTagName("option");
var rect = options[1].getBoundingClientRect();
返回一个矩形,但该矩形的所有属性都为零。我更喜欢直接的 JavaScript 解决方案,而不依赖于像 JQuery 这样的库,但如果必须的话,我会接受 JQuery 答案。
I have the following HTML code:
<html>
<head>
<title>Cheeses</title>
</head>
<body>
<select name="multi" id="multi" multiple="multiple">
<option selected="selected" label="emmental">Emmental</option>
<option label="roquefort" >Roquefort</option>
<option label="parmigiano">Parmigiano</option>
<option label="cheddar">Cheddar</option>
</select>
</body>
</html>
In JavaScript in IE, how do I get the dimensions and position of one of the <option>
elements? Doing the following doesn't work:
var selectEl = document.getElementById("multi");
var options = selectEl.getElementsByTagName("option");
var rect = options[1].getBoundingClientRect();
A rect is returned, but it has zero for all properties of the rect. I would prefer a straight JavaScript solution, not relying on a library like JQuery, but if I have to, I'd accept a JQuery answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选项不会有任何尺寸,包含的选择元素具有宽度和宽度。高度。
计算屏幕位置比较棘手,因为您需要计算出父元素的偏移宽度和高度。
jQuery 有计算这些值的方法,但它们并不总是那么准确。
The options won't have any dimensions, the containing select element has the width & height.
Calculating the screen position is trickier because you have the work out the offset widths and heights of the parent elements.
jQuery has methods that work out these values, but they aren't always that accurate.