javascript - 样式化选择框

标签 javascript html css jquery-mobile hybrid-mobile-app

我很难将页面顶部的选择框样式设置为看起来像下面的可折叠 DIV。当我的 API 返回 2 个对象时(当有 2 个选项可供选择时,我只需要进行此外观更改盒子)。我也不想使用一个插件,如果可以的话,它可以让你给按钮一个 onClick 函数来模拟下拉选择——这需要大量的重新开发等。

当有 2 个选项时,我想将它的样式设置为糟糕:

white plus sign added to the left of the select box

这是我用来设置按钮样式的一些代码:

var innerdiv = $('<div class="acc_list" data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="b" ></div>');

这为 DIV 添加了可折叠功能。

我已经尝试将选择框添加到 DIV 中,但它似乎是在右下角添加的。

 <div id="selectBoxContainer" data-role="fieldcontain">
              <select id="selectbox" class="combobox" name="selectbox" onchange="PolicyChange('selectbox')"></select>
          </div>

我检查数据是否包含两个对象,如果是:

  if (parseInt(policies.length) > 1)
{
    select.removeClass('combobox');
    changeBox.addClass('ui-collapsible-heading-toggle ui-btn ui-btn-icon-left ui-icon-plus ui-btn-b');
}

下面是我的 CSS 页面:

    /*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
* {
    -webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */
}

body {
    -webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
    -webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
    -webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
    background-color: #E4E4E4;
    background-image: linear-gradient(top, #A7A7A7 0%, #E4E4E4 51%);
    background-image: -webkit-linear-gradient(top, #A7A7A7 0%, #E4E4E4 51%);
    background-image: -ms-linear-gradient(top, #A7A7A7 0%, #E4E4E4 51%);
    background-image: -webkit-gradient( linear, left top, left bottom, color-stop(0, #A7A7A7), color-stop(0.51, #E4E4E4) );
    background-attachment: fixed;
    height: 100%;
    margin: 0px;
    padding: 0px;
    width: 100%;
    background: transparent !important;
    /* 170px x 200px */
    background-repeat: no-repeat;
    font-size: 15px;
    background-position: center top;
    background-size: 100% !important;
}

/* Portrait layout (default) */
.app {
    position: absolute; /* position in the center of the screen */
    height: 50px; /* text area height */
    width: 225px; /* text area width */
    text-align: center;
    padding: 180px 0px 0px 0px; /* image height is 200px (bottom 20px are overlapped with text) */
    margin: -115px 0px 0px -112px; /* offset vertical: half of image height and text area height */
    /* offset horizontal: half of text area width */
}

/* Landscape layout (with min-width) */
@media screen and (min-aspect-ratio: 1/1) and (min-width:400px) {
    .app {
        background-position: left center;
        padding: 75px 0px 75px 170px; /* padding-top + padding-bottom + text area = image height */
        margin: -90px 0px 0px -198px; /* offset vertical: half of image height */
        /* offset horizontal: half of image width and text area width */
    }
}

h1 {
}

.event {
    border-radius: 4px;
    -webkit-border-radius: 4px;
    color: #FFFFFF;
    font-size: 12px;
    margin: 0px 30px;
    padding: 2px 0px;
}

    .event.listening {
        background-color: #333333;
        display: block;
    }

    .event.received {
        background-color: #4B946A;
        display: none;
    }

@keyframes fade {
    from {
        opacity: 1.0;
    }

    50% {
        opacity: 0.4;
    }

    to {
        opacity: 1.0;
    }
}

@-webkit-keyframes fade {
    from {
        opacity: 1.0;
    }

    50% {
        opacity: 0.4;
    }

    to {
        opacity: 1.0;
    }
}

.centerize {
    display: block;
    margin: 0 auto;
    text-align: center;
}

.img_grey {
    display: block;
    -webkit-filter: grayscale(100%);
}

.blink {
    animation: fade 3000ms infinite;
    -webkit-animation: fade 3000ms infinite;
}

.ui-btn.ui-input-btn, .ui-link.ui-btn {
    display: inline-block;
    vertical-align: bottom;
}

.ui-block-a, .ui-block-b, .ui-block-c {
    text-align: center !important;
}

.ui-content {
}

div[data-role="footer"] {
}

    div[data-role="footer"] .nav-glyphish-example .ui-btn .ui-btn-inner {
        padding-top: 40px !important;
    }

    div[data-role="footer"] .nav-glyphish-example .ui-btn .ui-icon:after {
        width: 30px !important;
        height: 30px !important;
        margin-left: 15px !important;
        box-shadow: none !important;
        -moz-box-shadow: none !important;
        -webkit-box-shadow: none !important;
        -webkit-border-radius: 0 !important;
        border-radius: 0 !important;
    }


#home.ui-btn-icon-top:after {
    background-position: 10% 10%;
    background-size: 100% 100% !important;
    background-image: url('../img/home-white.png') !important;
    background-repeat: no-repeat !important;
    background-attachment: scroll !important;
}

#call.ui-btn-icon-top:after {
    background-position: 10% 10%;
    background-size: 100% 100% !important;
    background-image: url('../img/contact-white.png') !important;
    background-repeat: no-repeat !important;
    background-attachment: scroll !important;
}

#settings.ui-btn-icon-top:after {
    background-position: 10% 10%;
    background-size: 100% 100% !important;
    background-image: url('../img/settings-white.png') !important;
    background-repeat: no-repeat !important;
    background-attachment: scroll !important;
}

#panic.ui-btn-icon-top:after {
    background-position: 10% 10%;
    background-size: 100% 100% !important;
    margin-top: 5px;
    background-repeat: no-repeat !important;
    background-attachment: scroll !important;
}

#panicafr.ui-btn-icon-top:after {
    background-position: 10% 10%;
    background-size: 100% 100% !important;
    margin-top: 5px;
    background-image: url('../img/panicafr.png') !important;
    background-repeat: no-repeat !important;
    background-attachment: scroll !important;
}

a#panic.panicAfr:after {
    background-position: 10% 10%;
    background-size: 100% 100% !important;
    margin-top: 5px;
    background-image: url('../img/panicafr.png') !important;
    background-repeat: no-repeat !important;
    background-attachment: scroll !important;
}

a#panic.panicEng:after {
    background-position: 10% 10%;
    background-size: 100% 100% !important;
    margin-top: 5px;
    background-image: url('../img/panic.png') !important;
    background-repeat: no-repeat !important;
    background-attachment: scroll !important;
}

a#panic.panicAfrSkip:after {
    background-position: 10% 10%;
    background-size: 100% 100% !important;
    margin-top: 5px;
    background-image: url('../img/panicgreyafr.png') !important;
    background-repeat: no-repeat !important;
    background-attachment: scroll !important;
}

a#panic.panicEngSkip:after {
    background-position: 10% 10%;
    background-size: 100% 100% !important;
    margin-top: 5px;
    background-image: url('../img/panicgrey.png') !important;
    background-repeat: no-repeat !important;
    background-attachment: scroll !important;
}

.ui-grid-b > .ui-block-a p, .ui-grid-b > .ui-block-b p, .ui-grid-b > .ui-block-c p {
    margin: 5px 0 20px 0;
}

.backbutton {
    background-image: url(../img/back.png);
    background-repeat: no-repeat;
    background-position: left;
    padding-left: 25px !important;
    background-size: auto 100%;
    color: #0096ff !important;
    background-color: transparent !important;
    background-color: transparent !important;
    border-color: transparent !important;
    width: 0.6em;
    height: 0.6em;
    padding-top: 0.2em;
}

.loginbutton {
    background-image: url(../img/login-skip.png);
    background-repeat: no-repeat;
    background-position: right;
    padding-left: 25px !important;
    background-size: auto 100%;
    color: #0096ff !important;
    background-color: transparent !important;
    background-color: transparent !important;
    border-color: transparent !important;
    width: 10px;
    height: 10px;
    padding-top: 5px;
}

.logo {
    text-align: center;
}

li {
    padding-left: 2px;
}

.acc_list a:after {
    border-radius: 4em !important;
    width: 2.5em !important;
    height: 2.5em !important;
    left: 0.15em;
    top: 1em;
}


div#backHeader * {
    vertical-align: middle;
    line-height: 1.5em;
}

.lbl-no-float {
    float: initial !important;
}

div.loadingModal * {
}

div.loadingModal img {
    margin-top: 2em;
}

.infoimg {
    width: 1em;
    height: 1em;
}

div.loadingModal p:after {
    content: '...';
}

div.loadingModal p {
    padding-left: 0.5em;
}

#img_login_skip {
    position: absolute;
    right: 0;
    padding-right: 0.2em;
    top: -0.35em;
}

div.ui-block-b.userInfo label {
    display: block;
    width: 100%;
    text-align: left;
}

fieldset { 
    display: block;
    margin-left: 2px;
    margin-right: 2px;
    padding-top: 0.35em;
    padding-bottom: 0.625em;
    padding-left: 0.75em;
    padding-right: 0.75em;
    border: 2px;
}

hr { width:100%; height:1px; background: #fff }

a.ui-collapsible-heading-toggle.ui-btn.ui-icon-plus.ui-btn-icon-left.ui-btn-b,
a.ui-collapsible-heading-toggle.ui-btn.ui-btn-icon-left.ui-btn-b.ui-icon-minus {
    background-image: none !important;
    background-color: rgb(237, 237, 237) !important;
}

.profileimagesection {
    margin: 0 auto;
    overflow: hidden;    
    margin-bottom: 1em;
}

.pressedIconsContainerInactive{
    opacity: 1.0 !important;
    -moz-opacity: 1.0 !important;
    -webkit-opacity: 1.0 !important;
    filter: alpha(opacity=100) !important; /*IE compatibility for Windows Phones (possihttp://localhost:59778/IntegriSure/css/index.cssbly needed)*/
}

.pressedIconsContainerActive{
    opacity: 0.7 !important;
    -moz-opacity: 0.7 !important;
    -webkit-opacity: 0.7 !important;
    filter: alpha(opacity=70); /*IE compatibility for Windows Phones (possibly needed)*/
}


/*.userProfilePic{
    background-size:cover;
    width: 40vw; /*use CSS3 viewports to keep  size on Tablets*/
/*    height: 25vh; /*use CSS3 viewports to keep size on Tablets*/
/*    border: 1px solid #eee;
    border-radius: 5px;
}*/

.inactiveLink {
   pointer-events: none;
   cursor: default;
}

.ui-icon-loading {
    background: url('../img/loading-spin.svg') !important;
    background-size: contain !important;
}

.ui-loader-default {
    opacity: 1.0 !important;
}

.ui-btn-inline {
    margin-right: 0 !important;
}

#claimstracking .ui-field-contain label:last-child{
    width:auto;
}

.ui-listview a {
    display: block;
    /* max-width: 80%; */
    word-wrap: break-word;
    white-space: normal;
    text-decoration: none;
}

#pressdetails #pressData * {
    font-size:  12pt !important;
}

.toolbarinitial {
    position: initial;
}

input.red{
background-color: #FF0000;
border-color: #F04040;
}

input.green{
background-color: #00FF00;
border-color: #8DFF1C;
}

尝试以下响应后 - 我得到了白色加号,但它位于左下角 - 似乎无法使其与文本内联。很难在图像中看到,但十字在那里但不是内联:

Top of the cross on the bottom left of the text

还有什么建议吗?

最佳答案

jQM 选择小部件具有用于将图标放在左侧和选择要显示的图标的属性(data-iconpos="left"data-icon="plus"):

<div id="selectBoxContainer" data-role="fieldcontain">
      <select id="selectbox" class="combobox" name="selectbox" data-iconpos="left" data-icon="plus" >
          <option val="1">Option 1</option>
          <option val="2">Option 2</option>
      </select>          
</div>

然后,如果您想隐藏图标后面的灰色圆盘,以便只显示白色加号,请使用此 CSS:

#selectBoxContainer .ui-select .ui-btn-icon-left::after {
    background-color: transparent !important;
}

DEMO

关于javascript - 样式化选择框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33737048/

相关文章:

JavaScript:替换网页上的(所有)某些单词

javascript - 修复了 fullpage.js 部分内的 div

javascript - 如何在不使用 Grid.Select() 的情况下获取 Kendo Grid 中一行的 UID

javascript - jstree 显示选定的子条目

javascript - 使用 JQuery 刷新页面的一部分

javascript - 在 javascript 中生成表单时出现问题 [HTML5]

html - 有没有更好的方法来完成这个看似简单的三栏布局呢?

css - 对齐两个 div,一个在顶部,一个在第三个 div 的底部

css - @font-face 渲染为 Times New Roman

javascript - JQuery/Ajax 在 Razor 页面中发布并重定向到从 MVC Action(又名表单提交)返回的 View