当鼠标移到该选项上时,如何获取要选择的多选对象的选项
如何设置当鼠标移动到索引选项上时要选择的多选对象的索引。例如在接下来的html
代码中,当鼠标移到选项1上时,它将被选中。我想在 JavaScript 中完成这一切,而不需要编辑 html
代码。
<select size="6" multiple="multiple">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5</option>
<option value="6">option 6</option>
</select>
How to set index of a multiple select object to be selected when mouse moves over that index's option. for example in the next html
code, when mouse move over option 1, it will be selected. I want to do it all in JavaScript without editing the html
code.
<select size="6" multiple="multiple">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5</option>
<option value="6">option 6</option>
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发生的情况是,资源管理器不会为选项标签触发任何事件,也不允许我们获取其坐标或尺寸,因此我认为唯一要做的就是欺骗浏览器:
选择对象的子对象是选项...我猜有些是文本
节点,因为我们使用空格来缩进 HTML,所以我使用
第二个孩子(选项[1])以获得对
第一个 [option] 元素。
显示,不会影响内容。
字体大小样式(这是技巧,我正在尝试计算
[option] 元素的高度及其字体大小。有时这个
值以点(例如 10pt)为单位指定,因此我正在创建一个 div
完全相同的高度值并要求浏览器给出
返回给我的高度(以像素为单位)。一旦我有了高度
[option] 元素,其余的都是微不足道的。
鼠标的高度除以 [option] 元素的高度。这
将为我们提供鼠标当前所在的元素
(鼠标顶部位置 - 选择顶部位置从屏幕转换
坐标到选择框坐标并除以高度
[option] 元素的 给我们当前的 [option]。
选定的索引。
代码:
对于其余浏览器,处理要简单得多:
不要忘记为 [select] 提供正确的 id (id="mySelect") &在 [select] 上添加 onmousemove="select( event )" 。
这对我有用:Chrome,FireFox(3.6),Explorer 8,Explorer 6(模拟),Opera&amp;野生动物园。
记得在完成后将测试 DIV 从文档中删除,否则 DOM 中将会出现一堆未使用的 DIV,因此在 ieElementFromPoint() 末尾添加:
document.body.removeChild( d );
希望这有帮助。
What happens is that explorer does not fire any events for the options tag, nor does it allow us to get its coordinates or dimensions so i think that the only thing left to do is trick the browser a little:
children of the select object are options... i guess some are text
nodes because we use white space to indent the HTML, so i'm using the
second child (options[1]) in order to obtain a reference to the
first [option] element.
displayed and won't effect the content.
font-size style (this is the trick, i'm trying to calculate the
height of the [option] element by its font-size. sometimes this
value is specified in points (e.g. 10pt) so i'm creating a div with
exactly the same value for its height and asking the browser to give
the height back to me in pixels. Once i have the height of the
[option] element, the rest is trivial.
of the mouse and divide by the height of the [option] element. This
will give us the element on which the mouse is currently positioned
(mouse top position - select top position converts from screen
coordinates to the select box coordinates and dividing by the height
of the [option] element gives us the current [option].
selectedIndex.
Code:
For the rest of the browsers the treatment is much simpler:
Don't forget to give the [select] the proper id (id="mySelect") & add onmousemove="select( event )" on the [select] as well.
This worked for me on: Chrome, FireFox (3.6), Explorer 8, Explorer 6 (emulated), Opera & Safari.
Remember to remove the test DIV from the document when we're done with it, otherwise there will be a bunch of unused DIVs in the DOM, so at the end of ieElementFromPoint() add:
document.body.removeChild( d );
Hope this help.
您可以使用 jQuery:
You can use jQuery:
如果你想给 select 元素一个 id 为“mySelect”的
简单 JavaScript,它会像这样实现:
If you like to give the select element an id of "mySelect"
With plain javascript, it would be implemented like this: