简体   繁体   English

如何在 safari 中以编程方式隐藏打开的选择框中的选项?

[英]How can I hide the options in an open select box programmatically in safari?

Here's a jsfiddle: http://jsfiddle.net/JQnUs/4/这是一个 jsfiddle: http : //jsfiddle.net/JQnUs/4/

HTML: HTML:

<div class="problem-select">
    <select>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
    <input type="hidden" value="1" />    
</div>

<div class="overlapping-div">

</div>

<div class="hover-here">Hover here</div>

<input class="other-input" type="text" />

CSS: CSS:

.problem-select {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 0;
}

.overlapping-div {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100px;
    height: 100px;
    background-color: red;
    z-index: 100;
    display: none;
}

.hover-here {
    position: absolute;
    right: 70px;
    top: 0px;
    border: 1px solid black;
    height: 100px;
    width: 100px;
}

.other-input {
    position: absolute;
    top: 200px;
    left: 0px;
}

jQuery (using 1.8.2): jQuery(使用 1.8.2):

jQuery(document).ready(function() {
    jQuery(".hover-here").hover(function(){ showDiv(); }, function(){ hideDiv(); });
    //Used in potential solution 7...
    //jQuery("select").change(function(){ jQuery(this).siblings('input').val(jQuery(this).val()); });
});

function showDiv() {
    //Potential solution number 1... (does't work in chrome or safari)
    //jQuery('select').blur();

    //Potential solution number 2... (leaves lines on top of the red div)
    //jQuery('option').hide();

    //Potential solution number 3... (doesn't work in safari and still has the issue of working out which selects to hide)
    //jQuery('select').hide();

    //Potential solution number 4... (doesn't hide drop down options at all and would also need to know which selects to disable)
    //jQuery('select').attr('disabled','disabled');

    //Potential solution number 5... (doesn't work at all)
    //jQuery(".hover-here").click();

    //Potential solution number 6... (doesn't work in safari or chrome)
    //jQuery('.other-input').focus();

    //Potential solution number 7... (doesn't work in safari)
    //innerHtml = jQuery('.problem-select').html();
    //value = jQuery('.problem-select').children("input").val();
    //jQuery('.problem-select').html(innerHtml);
    //jQuery('.problem-select').children("select").val(value);


    jQuery('.overlapping-div').show();
}

function hideDiv() {
    //Potential solution number 2 cont...
    //jQuery('option').show();

    //Potential solution number 3 cont...
    //jQuery('select').show();

    //Potential solution number 4 cont...
    //jQuery('select').attr('disabled',null);

    jQuery('.overlapping-div').hide();
}

If you click on the select box to open it, then hover over the div to the right without selecting an option from the select box or clicking elsewhere on the page first, then the select options still show up on top of the red div that appears.如果您单击选择框将其打开,然后将鼠标悬停在右侧的 div 上而不从选择框中选择选项或先单击页面上的其他位置,则选择选项仍会显示在出现的红色 div 的顶部.

Some of the things I've tried are in the fiddle and and the relevant sections just need to be uncommented to see what happens with them.我尝试过的一些事情在小提琴中,相关部分只需要取消注释即可查看它们会发生什么。 Most of them work in firefox and none of them work in safari.他们中的大多数人在 Firefox 中工作,而没有一个人在 safari 中工作。

An additional issue that I am facing in the actual application is that the select box may or may not be underneath the red div, so the solution needs either to be able to determine if the select box options will obscure the red div or continue to allow normal use (selecting options).我在实际应用程序中面临的另一个问题是选择框可能位于也可能不在红色 div 下方,因此解决方案需要能够确定选择框选项是否会遮住红色 div 或继续允许正常使用(选择选项)。 (In the application it is possible to keep the red div visible whilst doing other actions on the page). (在应用程序中,可以在页面上执行其他操作的同时保持红色 div 可见)。

How can I stop the options list showing over the red div in chrome, safari, ie7-9 and firefox whilst not affecting the functionality of the select box if it is not covered by the red div?如果选择框未被红色 div 覆盖,如何停止在 chrome、safari、ie7-9 和 firefox 中显示在红色 div 上的选项列表,同时不影响选择框的功能?

I am trying to avoid using a plugin that recreates a select box using other html elements to do this as in my experience they don't work well with applications that change the functions that get triggered on various events regularly.我试图避免使用使用其他 html 元素重新创建选择框的插件来执行此操作,因为根据我的经验,它们不适用于更改定期触发各种事件的功能的应用程序。

I ran into the exact same problem while testing an application in Mobile Safari.我在 Mobile Safari 中测试应用程序时遇到了完全相同的问题。 Apparently .hide() does not work because Mobile Safari changes a dropdown select into a "pop over" select.显然 .hide() 不起作用,因为 Mobile Safari 将下拉选择更改为“弹出”选择。 As a compromise, I chose to simply disable the unwanted options:作为妥协,我选择简单地禁用不需要的选项:

    $(document).ready(function(){
        $("#yourSelectDiv").change(function(){
        //target an attribute from your first drop down)
        if ( $(“#yourSelectID option:selected”).attr(“data-custom”) == ’yourCustomAttribute’){
        //reset the second drop down to default position
        $(“#yourOtherSelectID”).get(0);
        //set disabled to true or false on the second drop down 
        $("#yourOtherSelectID option[value='yourSelectValue']").attr('disabled',false);
        $("#yourOtherSelectID option[value='yourSelectValue']").attr('disabled’,true);
    });
    });

I found a possible solution:我找到了一个可能的解决方案:

On hover of the .hover-here div, I add a class to the select that adds the following rules:在 .hover-here div 悬停时,我向 select 添加了一个类,该类添加了以下规则:

.hide-select {
    position: absolute;
    overflow: hidden;
    clip: rect(0 0 0 0);
    height: 1px;
    width: 1px;
    margin: -1px;
    padding: 0;
    border: 0;
}

(taken from this page ) (取自本页

Haven't tested it in all browsers yet, but in Chrome and IE9 it seems to work fine :)尚未在所有浏览器中对其进行测试,但在 Chrome 和 IE9 中似乎可以正常工作:)

Test for yourself: http://jsfiddle.net/JQnUs/6/自己测试: http : //jsfiddle.net/JQnUs/6/

edit Doesn't seem to do so well in IE9 after all, it still functions but the dropdown is getting messed up afterwards.. looking for a fix.编辑毕竟在 IE9 中似乎没有那么好,它仍然可以运行,但之后下拉菜单变得混乱了.. 寻找修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM