简体   繁体   English

图片 <option>适用于Firefox,但不适用于其他浏览器

[英]Image for <option> works in firefox but not works in other browsers

html: HTML:

<select size="10" style="width: 325px; background-color: #fff;" class="postCode">
    <option value="36621746_0S_F">6 Upperkirkgate Aberdeen</option>
    <option value="31560744_0S_F">12A Upperkirkgate Aberdeen</option>
    <option value="31560745_0S_F">12B Upperkirkgate Aberdeen</option>
    <option value="36621735_0S_F">36 Upperkirkgate Aberdeen</option>
    <option value="35390362_0S_F">48-58 Upperkirkgate Aberdeen</option>
</select>

Css: CSS:

.postCode {
    background-color: rgba(0, 0, 0, 0);
}
.postCode option
{
    background-color: rgba(0, 0, 0, 0);
    background-image: url('../img/house.png');
    background-repeat: no-repeat;
    background-position: left top;
    text-indent: 2em;
}

In Firefox 在Firefox中

在Firefox中

In Chrome 在Chrome中

在Chrome中

Please help me. 请帮我。

UPD: - IE8 in the same result as in Chrome UPD: - IE8与Chrome中的结果相同

Some browsers implement drop down menus using native controls and do not allow options to be styled. 某些浏览器使用本机控件实现下拉菜单,并且不允许对选项进行样式设置。 Short of replacing the element with a collection of other elements and a stack of JavaScript, there is nothing you can do about this. 如果用一组其他元素和一堆JavaScript替换元素,那么你无能为力。

Maybe try 也许试试吧

-webkit-appearance:none;

and

-webkit-box-sizing: contents-box;

There is more discussion about the subject here 有关于某个主题的讨论, 在这里

I do it with lists 我用列表做

JS: JS:

(function ($) {
    $.fn.postcodeAddressList = function() {

        this.each(function() {
            var el = $(this),
            li = el.find('li'),
            i = li.length;

            strippedList(el);

            while(i--) {
                $(li[i]).on("click", function(e){ 
                    strippedList(el);
                    $(e.currentTarget).attr("selected", "selected");
                    el.attr("data",$(e.currentTarget).attr("data"));
                });
            } 
        });

        return this;
    };
})(jQuery);

strippedList =  function ( el) {
    var li = el.find('li'),
        i = li.length; 

     while(i--) {
         if($( li[i] ).attr("selected")!= 'defined') {
             $(li[i]).removeAttr("selected");
         }
         if (i%2) {
             $(li[i]).attr("stripped","stripped");
         }else {
             $(li[i]).removeAttr("stripped");
         }
     }

}

CSS: CSS:

.postCode li[stripped="stripped"] {
    background-color: #f5f5f5;
}
.postCode li[selected="selected"] {
    background-color: #ccc;
}
.postCode li
{
    background-image: url('../img/house.png');
    background-repeat: no-repeat;
    background-position: 5% 50%;
    padding: 7px 0 7px 4em;
    cursor: pointer;
    font-weight: bold;
    color: #333;
}
.postCode[disabled="disabled"] {
    background-color: #f5f5f5;
    border-color: #ddd;
    cursor: not-allowed;
}
.postCode {
    background-color: white;
    list-style: none outside none;
    margin-left: -10px;
    padding: 5px 0;
    width: 340px;
    height: 300px;
    overflow-y: auto;
    border: solid 1px #CCC;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
}

HTML: HTML:

<ul class="postCode" data="29392449_0S_F">
    <li data="26850525_898354S_F">Andrew Begg 24-26 Upperkirkgate Aberdeen</li>
    <li data="31763873_0S_F" stripped="stripped">Flat 1 12 Upperkirkgate Aberdeen</li>
    <li data="31763875_0S_F">Flat 1 14 Upperkirkgate Aberdeen</li>
    ...
</ul>

Use: 使用:

...
$('.postCode').postcodeAddressList();
...


...
var value = $('.postCode').attr("data");
...

Result: 结果:

Mozilla Mozilla的

在此输入图像描述

Chrome 在此输入图像描述

IE8 IE8 在此输入图像描述

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

相关问题 脚本适用于其他浏览器但不适用于Firefox - Script works in other browsers but not in Firefox 该网站可以在Firefox中运行,但没有其他浏览器,为什么? - site works in firefox but no other browsers, why? 该网站可以在除Firefox之外的其他浏览器上正常运行 - Site works fine on other browsers except for Firefox Firefox无法识别Java代码,但可以在所有其他浏览器中使用 - Javascript code not understood by Firefox But works in all other browsers Firefox无法播放声音,但可以在其他浏览器中使用 - Firefox won't play sounds, but works in other browsers $ .post不适用于最新的Firefox,但可用于所有其他浏览器 - $.post not working in latest firefox but works in all other browsers 在Firefox中使用Hammer Pinch方向错误-在所有其他浏览器中均可使用 - Using Hammer Pinch in Firefox directed wrongly - works in all other browsers Facebook类似于按钮iframe的实现在Firefox中无法点击,可在其他浏览器中使用 - Facebook like button iframe implementation not clickable in Firefox, works in other browsers Javascript文档[“ someid”]。src在Firefox中有效,但在其他浏览器中无效 - Javascript document[“someid”].src works in Firefox, but not in other browsers 可以在firefox中使用但在所有其他浏览器中均无法使用的jquery.ajax代码 - jquery.ajax code that works in firefox but fails in all other browsers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM