当鼠标移到该选项上时,如何获取要选择的多选对象的选项

发布于 2024-12-23 02:30:09 字数 562 浏览 0 评论 0原文

如何设置当鼠标移动到索引选项上时要选择的多选对象的索引。例如在接下来的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心如荒岛 2024-12-30 02:30:09

发生的情况是,资源管理器不会为选项标签触发任何事件,也不允许我们获取其坐标或尺寸,因此我认为唯一要做的就是欺骗浏览器:

  1. 使用它的 ID。
  2. 接下来,获取第一个 [option] 对象(由于某种原因,并非所有
    选择对象的子对象是选项...我猜有些是文本
    节点,因为我们使用空格来缩进 HTML,所以我使用
    第二个孩子(选项[1])以获得对
    第一个 [option] 元素。
  3. 创建一个虚拟 div 元素,它将用作隐藏画布。
  4. 将 div 设置为绝对位置 &可见性隐藏所以我们不会
    显示,不会影响内容。
  5. 将 div 的高度设置为 [option] 标记中指定的大小
    字体大小样式(这是技巧,我正在尝试计算
    [option] 元素的高度及其字体大小。有时这个
    值以点(例如 10pt)为单位指定,因此我正在创建一个 div
    完全相同的高度值并要求浏览器给出
    返回给我的高度(以像素为单位)。一旦我有了高度
    [option] 元素,其余的都是微不足道的。
  6. 从 y 坐标中减去选择的最顶部位置
    鼠标的高度除以 [option] 元素的高度。这
    将为我们提供鼠标当前所在的元素
    (鼠标顶部位置 - 选择顶部位置从屏幕转换
    坐标到选择框坐标并除以高度
    [option] 元素的 给我们当前的 [option]。
  7. 获取当前[选项]的编号并将其用作
    选定的索引。

代码:

    function ieElementFromPoint( e )
    {
        var select  = document.getElementById( "mySelect" );
        var options = select.childNodes;
        var d = document.createElement( "DIV" );
        d.style.position = "absolute";
        d.style.visibility = "hidden";
        d.style.height = options[ 1 ].currentStyle.fontSize;
        document.body.appendChild( d );
        select.selectedIndex = ( Math.round( ( ( e.clientY + document.body.scrollTop ) - select.offsetTop ) / d.offsetHeight ) );
     }

对于其余浏览器,处理要简单得多:

var old = null;

function select( e )
{
    if ( document.all )
    {
        ieElementFromPoint( e );
    }
    else
    {
        var option = e.target;

        if ( option.tagName == "OPTION" )
        {
            if ( old != null )
            {
                old.selected = false;
            }

            old = option;
            option.selected = true;
        }
    }
}

不要忘记为 [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:

  1. Get the [select] object using its ID.
  2. Next, get the first [option] object (for some reason not all of the
    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.
  3. Create a dummy div element which will be used as a hidden canvas.
  4. Set the div to absolute position & visibility hidden so it won't we
    displayed and won't effect the content.
  5. Set the div's height to the size specified in the [option] tag
    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.
  6. subtract the top most position of the select from the y coordinate
    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].
  7. Take the number of the current [option] and use it as the value for
    selectedIndex.

Code:

    function ieElementFromPoint( e )
    {
        var select  = document.getElementById( "mySelect" );
        var options = select.childNodes;
        var d = document.createElement( "DIV" );
        d.style.position = "absolute";
        d.style.visibility = "hidden";
        d.style.height = options[ 1 ].currentStyle.fontSize;
        document.body.appendChild( d );
        select.selectedIndex = ( Math.round( ( ( e.clientY + document.body.scrollTop ) - select.offsetTop ) / d.offsetHeight ) );
     }

For the rest of the browsers the treatment is much simpler:

var old = null;

function select( e )
{
    if ( document.all )
    {
        ieElementFromPoint( e );
    }
    else
    {
        var option = e.target;

        if ( option.tagName == "OPTION" )
        {
            if ( old != null )
            {
                old.selected = false;
            }

            old = option;
            option.selected = true;
        }
    }
}

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.

烟雨凡馨 2024-12-30 02:30:09

您可以使用 jQuery:

$("option").mouseover(function(){
    $(this).prop("selected",true);
});

You can use jQuery:

$("option").mouseover(function(){
    $(this).prop("selected",true);
});
反话 2024-12-30 02:30:09

如果你想给 select 元素一个 id 为“mySelect”的

<select size="6" multiple="multiple" id ="mySelect">
                  ...
</select>

简单 JavaScript,它会像这样实现:

<script type = "text/javascript">

var element = document.getElementById("mySelect");
var options = element.options;
for(var i = 0; i<options.length; i++){
    options.item(i).onmouseover = function(e){
        e.target.parentNode.selectedIndex = e.target.value-1;
    };
}

</script>

If you like to give the select element an id of "mySelect"

<select size="6" multiple="multiple" id ="mySelect">
                  ...
</select>

With plain javascript, it would be implemented like this:

<script type = "text/javascript">

var element = document.getElementById("mySelect");
var options = element.options;
for(var i = 0; i<options.length; i++){
    options.item(i).onmouseover = function(e){
        e.target.parentNode.selectedIndex = e.target.value-1;
    };
}

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文