Bad cursor in select/option, IE

后端 未结 2 937
清歌不尽
清歌不尽 2021-01-21 01:43

I have a problem with bad cursor in options when the text is under that.

Normally, the option uses \"default\" cursor, but when eg. the paragraph is under option, in IE

相关标签:
2条回答
  • 2021-01-21 02:36

    IE does not honour the z-index of the select object, and applies the cursor attribute of any underlying object.

    I handle this by changing the cursor attribute of all underlying objects to pointer at the onfocus event for the select object, and restore them at onblur event for the select object.

    It is disappointing that IE does not handle this correctly, as do most other browsers.

    Example code (as requested by DavidG):

    In the this example we apply a class of 'underselect' to all elements that will be covered by the select dropdown (n.b. the dropdown does not always expand downwards, it can expand upwards).

    On page load the initial cursor property of each element with the class 'underselect' is saved in a attribute 'ic' of that element.

    The focus event of the select element is bound to a function that sets the cursor of all elements with the class 'underselect' to pointer.

    The blur event of the select element is bound to a function that sets the cursor of all elements with the class 'underselect' to the initial value that was stored on page load.

    Using jquery

    $(function ()
    {
      $("#select")
        .bind("focus", function(){ $(".underselect").each(function(){$(this).css("cursor", "pointer") });  })
        .bind("blur", function(){ $(".underselect").each(function (i){$(this).css("cursor", $(this).data("ic")); }) });
    
      $(".underselect").each(function () { $(this).data("ic", $(this).css("cursor")); });
    });
    

    Using javascript only we create the following functions:

    function saveinitialcursor()
    {
      gp = document.getElementsByClassName("underselect");
      for (var i = 0, l = gp.length; i < l; i++)
      {
        style = getComputedStyle(gp[i]);
        cursor = style.getPropertyValue("cursor");
        gp[i].setAttribute("ic", cursor);
      }
    }
    
    function selectfocus()
    {
      gp = document.getElementsByClassName("underselect")
      for (var i = 0, l = gp.length; i < l; i++)
        gp[i].style.cursor = "pointer";
    }
    
    function selectblur()
    {
      gp = document.getElementsByClassName("underselect")
      for (var i = 0, l = gp.length; i < l; i++)
        gp[i].style.cursor = gp[i].getAttribute("ic");
    }
    

    and bind them to the body and select opening tags:

    <body onload="saveinitialcursor()" >
    
    <select id="select" onblur="selectblur()" onfocus="selectfocus()" >
    
    0 讨论(0)
  • 2021-01-21 02:40

    Although you've closed the select tag with </select>, you still need to close your options. At the moment the browser reads everything after: <option value=a selected="selected"> as a single option.

    Wrap all of your options with </option> - it should look like this:

    <form>
        <select>
            <option value=a selected="selected">First</option>
            <option value=b>Second</option>
            <option value=c>Third</option>
            <option value=c>Fourth</option>
        </select>
        <p>text</p>
    </form>
    
    0 讨论(0)
提交回复
热议问题