【问题标题】:Add flag image to select option添加标志图像以选择选项
【发布时间】:2018-12-12 19:09:21
【问题描述】:

美好的一天!我是新手。我试图在选择的选项标签中添加图像。但是当我运行代码时。它不显示图像。有人能帮我吗?谢谢

这是代码:

HTML:

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Custom select box Jquery Plugin by VJ</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">


      <link rel="stylesheet" href="css/style.css">


</head>

<body>

  <div class="container">

        <div class="selected-item">
            <p>You Selected Country : <span>Select</span></p>
        </div>

        <select name="" id="cusSelectbox">
            <option value="Select">Select</option>
            <option value="India"><img src="https://static.webshopapp.com/shops/094414/files/054959460/the-united-states-flag-icon-free-download.jpg">India</option>
            <option value="Nepal">Nepal</option>

        </select>



    </div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>



    <script  src="js/index.js"></script>




</body>

</html>

【问题讨论】:

标签: html css css-selectors html-select


【解决方案1】:

&lt;select&gt; & &lt;option&gt; 标签很难自定义

&lt;select&gt;&lt;option&gt; 标签很难定制,任何适用于一个浏览器的东西都不会适用于其他浏览器。

解决方案

  • 忘记自定义&lt;select&gt;,使用&lt;details&gt;&lt;summary&gt; 创建一个伪&lt;select&gt;

  &lt;select&gt;  &lt;details&gt; & &lt;summary&gt;

  • 忘记自定义&lt;option&gt;,使用&lt;input type='radio'&gt;&lt;label&gt; 创建一个伪&lt;option&gt;

  &lt;option&gt;  &lt;input type='radio'&gt; & &lt;label&gt;

&lt;select&gt; 和伪&lt;option&gt; 标签在外观和行为上将接近标准&lt;select&gt;&lt;option&gt; 标签。更重要的是,它可以存储和处理一个value,并且可以注册到change事件中。


演示

详情在demo中注释

/*| For Demonstration Purposes
This is a test to prove that this pseudo-<select> functions like 
a real <select> by logging the value of the selected pseudo-
<option> to the console.
*/

var xC = document.forms.container;
var xE = xC.elements;
var vR = xE.rad;

xC.onchange = function() {
  console.log(vR.value);
}
html,
body {
  font: 400 small-caps 16px/1.25 Arial;
}

fieldset {
  width: fit-content;
  padding: 0;
}

legend {
  font-size: 1rem
}

details {
  width: 150px;
  cursor: pointer;
  margin: 0 4px -5px 0;
  padding: 0 0 0 10px;
}

summary {
  position: relative;
  width: 96%;
  outline: 0.5px ridge grey;
}


/*
Hides <detail>'s default arrow
*/

details summary {
  list-style: none;
}

details summary::marker {
  display: none;
}


/*| Pseudo-<option>
All pseudo-<option> are initially hidden and 
<label class='opt'> are the only tags that will show/hide, 
the next comment explains how.
*/

.rad {
  display: none
}

.opt {
  display: none;
  margin: 0 0 2px 0;
  cursor: pointer;
  font-size: 0.9rem;
  box-shadow: -2px -2px 11px rgba(0, 0, 0, 0.3) inset;
}


/*| Two Conditions
1. If <details open='true'> all <label class='opt'> are visible.
=============================================
2. if <input class='rad' type='radio' checked> then the 
   <label class='opt'> that proceeds the radio button is visible.
*/

[open] .opt,
.rad:checked+.opt {
  display: block;
}


/*| For Demonstration Purposes
This ruleset changes how the console is displayed.
*/

.as-console-wrapper {
  width: 50%;
  min-height: 100%;
  margin-left: 50%;
  font-variant: normal;
}

.as-console-row.as-console-row::after {
  content: '';
  padding: 0;
  margin: 0;
  border: 0;
  width: 0;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Custom select box Jquery Plugin by VJ</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  <!--| Flag Icons
This stylesheet provides the flag icons. 
For details, go to: 
https://afeld.github.io/emoji-css/
-->

  <link href="https://afeld.github.io/emoji-css/emoji.css" rel="stylesheet" />
</head>

<body>
  <form id="container">
    <fieldset>
      <legend>Country</legend>

      <!--| Pseudo-<select> 
<details> provides the dropdown behavior and
<summary> contains the pseudo-<option>
-->
      <details>
        <summary>

          <!--| 4 Pseudo-<option> 
Each <label> and <input type='radio'> pair are
synced to each other by matching the values of
<label for="ID"> and <input id="ID">. 
-->

          <!--| Trigger and State
When <label> is clicked ... <input type='radio'>
is checked. This simple "cause and effect" can
be leveraged into a system of states (ie off/on). 
For details, review the CSS. 
-->Origin
        </summary>
        <input id='X' type='radio' class='rad' name='rad' value="" checked>
        <label class='opt' for='X'>
    &nbsp;&nbsp;╍╍╍╍╍╍╍╍╍
  </label>

        <input id='US' type='radio' class='rad' name='rad' value="United States">
        <label class='opt' for='US'>
    &nbsp;&nbsp;
    <i class='em em-us'></i> 
    United States
  </label>

        <input id='GB' type='radio' class='rad' name='rad' value="Great Britain">
        <label class='opt' for='GB'>
    &nbsp;&nbsp;
    <i class='em em-gb'></i> 
    Great Britain
  </label>

        <input id='IN' type='radio' class='rad' name='rad' value="India">
        <label class='opt' for='IN'>
    &nbsp;&nbsp;
    <i class='em em-flag-in'></i>
    India
  </label>

        <input id='NP' type='radio' class='rad' name='rad' value="Nepal">
        <label class='opt' for='NP'>
    &nbsp;&nbsp;
    <i class='em em-flag-np'></i>
    Nepal
  </label>


      </details>
    </fieldset>
  </form>

</body>

</html>

【讨论】:

  • 非常感谢您! :)
  • 如果我选择英国,我希望在输入收音机中检查英国而不是 ---------?
  • 嗨@rrr,您指的是#X 上的[checked] 属性吗?这只是用来设置最初选择的内容,因此如果您希望在加载页面后选择#GB,则将[checked] 属性添加到#GB 并从#X 中删除[checked] 属性。事实上,如果你根本不需要#X,你可以完全删除它。
  • 你好。因为我会将它用于语言切换器。例如,我选择了英语默认值。但其他人呢?如果我在刷新时单击 gb,我希望 gb 作为默认选择而不是英语
  • 你能帮我解决一下吗?我真的很抱歉:(
猜你喜欢
  • 2022-01-27
  • 1970-01-01
  • 2013-05-10
  • 2019-10-03
  • 2018-12-13
  • 2019-06-17
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
相关资源
最近更新 更多