2013-05-10 97 views
0

使用HTML select菜單,我試圖創建一個HTML元素,而不是純文本的下拉列表,但每個元素isn't displaying properly inside the dropdown menu是否可以在下拉列表中包含HTML元素?

<select> 
    <option value="1"> 
    <input type = "button" value = "This button won't display properly." /> 
    </option> 
    <option value=""> 
    <b>Bold text won't display properly either.</b> 
    </option> 
</select> 

是否有嵌入下拉內的HTML元素的任何語法上是有效的方法菜單?如果使用<select>菜單無法執行此操作,那麼如何創建一個允許嵌套HTML元素的下拉小部件?

+0

你需要選擇做'select'定製做 – 2013-05-10 20:52:15

+1

使用li/ul標籤和css – Sebas 2013-05-10 20:52:26

+0

@Sebas我不確定那會怎樣。你能詳細解釋一下嗎? – 2013-05-10 20:55:59

回答

1

下面是如何通過構建您自己的下拉處理程序來實現它的示例。

CSS

.select { 
    width: 10em; 
    height: 1em; 
    border-style: solid; 
    border-width: 1px; 
    border-radius: 3px; 
    z-index: 0; 
} 
.gradient1 { 
    background-color: #ebebeb; 
    background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#ebebeb), to(#999999)); 
    background-image: -webkit-linear-gradient(top, #ebebeb, #999999); 
    background-image: -moz-linear-gradient(top, #ebebeb, #999999); 
    background-image: -ms-linear-gradient(top, #ebebeb, #999999); 
    background-image: -o-linear-gradient(top, #ebebeb, #999999); 
} 
.gradient2 { 
    background-color: #999999; 
    background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#ebebeb), to(#ebebeb)); 
    background-image: -webkit-linear-gradient(top, #999999, #ebebeb); 
    background-image: -moz-linear-gradient(top, #999999, #ebebeb); 
    background-image: -ms-linear-gradient(top, #999999, #ebebeb); 
    background-image: -o-linear-gradient(top, #999999, #ebebeb); 
} 
.list { 
    position: relative; 
    margin-left: -1px; 
    padding: 0% 0% 0% 1px; 
    width: 100%; 
    height: auto; 
    box-shadow: 0px 5px 10px #888888; 
    border-style: solid; 
    border-width: 1px; 
    border-radius: 3px; 
    background-color: #ebebeb; 
    display: none; 
    margin-top: -4px; 
    z-index: 2; 
} 
.option { 
    display: block; 
    list-style-type: none; 
    text-align: left; 
} 
.option:hover { 
    background-color: blue; 
} 
.arrowDown { 
    position: relative; 
    top: -50%; 
    left: 90%; 
    width: 0%; 
    height: 0%; 
    border-left: 6px solid transparent; 
    border-right: 6px solid transparent; 
    border-top: 6px solid #444; 
} 
.value { 
    position: relative; 
    top: -2px; 
    left: 0%; 
    width: 100%; 
    height: 100%; 
    z-index: 1; 
} 

HTML

<div> 
    <div id="first" class="select"> 
     <div class="value"></div> 
     <div class="arrowDown"></div> 
     <ul class="list"> 
      <li class="option"><b>one</b></li> 
      <li class="option"><strike>two</strike></li> 
      <li class="option"><em>three</em></li> 
     </ul> 
    </div> 
    <div id="second" class="select"> 
     <div class="value"></div> 
     <div class="arrowDown"></div> 
     <ul class="list"> 
      <li class="option"><b>four</b></li> 
      <li class="option"><strike>five</strike></li> 
      <li class="option"><em>six</em></li> 
     </ul> 
    </div> 
    <div id="third" class="select"> 
     <div class="value"></div> 
     <div class="arrowDown"></div> 
     <ul class="list"> 
      <li class="option"><b>seven</b></li> 
      <li class="option"><strike>eight</strike></li> 
      <li class="option"><em>nine</em></li> 
     </ul> 
    </div> 
    <button id="getValue1">Get Text value 1</button> 
    <button id="getValue2">Get HTML value 2</button> 
    <button id="getValue3">Get Text value 3</button> 
</div> 

的Javascript

(function (global) { 
    global.addEventListener("load", function onLoad() { 
     global.removeEventListener("load", onLoad); 

     var selects = document.getElementsByClassName("select"); 

     function getTextValue(selectId) { 
      var select = document.getElementById(selectId), 
       values, 
       text = ""; 

      if (select) { 
       values = select.getElementsByClassName("value"); 
       if (values && values.length) { 
        text = values[0].textContent; 
       } 
      } 

      return text; 
     } 

     function getHTMLValue(selectId) { 
      var select = document.getElementById(selectId), 
       values, 
       html = ""; 

      if (select) { 
       values = select.getElementsByClassName("value"); 
       if (values && values.length) { 
        html = values[0].innerHTML; 
       } 
      } 

      return html; 
     } 

     function hideAll(not) { 
      Array.prototype.forEach.call(selects, function (select) { 
       if (select !== not) { 
        Array.prototype.forEach.call(select.getElementsByClassName("list"), function (ul) { 
         ul.style.display = "none"; 
        }); 

        select.className = (select.className.replace(/gradient[12]/, "").trim() + " gradient1").trim(); 
       } 
      }); 
     } 

     document.addEventListener("click", hideAll, false); 

     Array.prototype.forEach.call(selects, function (select) { 
      select.className = (select.className.trim() + " gradient1").trim(); 

      var lists = select.getElementsByClassName("list"), 
       options, 
       values, 
       value; 

      if (lists && lists.length) { 
       options = lists[0].getElementsByClassName("option"); 
       if (options && options.length) { 
        values = select.getElementsByClassName("value"); 
        if (values && values.length) { 
         value = values[0]; 
         value.innerHTML = options[0].innerHTML; 

         Array.prototype.forEach.call(options, function (option) { 
          option.addEventListener("click", function clickOption() { 
           value.innerHTML = this.innerHTML; 
          }, false); 
         }); 
        } 
       } 
      } 

      select.addEventListener("click", function clickSelect(evt) { 
       evt.stopPropagation(); 
       hideAll(this) 

       var lists = this.getElementsByClassName("list"), 
        list; 

       if (lists && lists.length) { 
        list = lists[0]; 

        if (global.getComputedStyle(list).display === "none") { 
         list.style.display = "block"; 
        } else { 
         list.style.display = "none"; 
        } 
       } 

       if (this.className.indexOf("gradient1") !== -1) { 
        this.className = this.className.replace("gradient1", "gradient2"); 
       } else { 
        this.className = (this.className.replace(/gradient\d/, "").trim() + " gradient1").trim(); 
       } 
      }, false); 
     }); 

     document.getElementById("getValue1").addEventListener("click", function() { 
      console.log(getTextValue("first")); 
     }, false); 

     document.getElementById("getValue2").addEventListener("click", function() { 
      console.log(getHTMLValue("second")); 
     }, false); 

     document.getElementById("getValue3").addEventListener("click", function() { 
      console.log(getTextValue("third")); 
     }, false); 
    }, false); 
}(window)); 

jsfiddle

+0

這很有效,雖然它在元素嵌套時不起作用:http://jsfiddle.net/zUdja/75/我希望可以以某種方式解決此問題。 – 2013-05-11 01:16:13

+0

我並沒有將它與嵌套編寫在一起,它純粹是HTML選擇/選項元素的等價物的例子,但是具有將標記作爲值的能力。我確信有一些工作可以擴展並轉化爲某種菜單部件,但是我確定有更好的菜單部件(沒有什麼能阻止你按照你的意願去做) – Xotic750 2013-05-11 01:25:15

2

問:-is有嵌入一個下拉菜單

號內的HTML元素任何語法上有效的方法,這是不可能任何語義在這一點上。這是無效的HTML。選項內不能有HTML標籤。

允許的內容:帶有最終轉義字符的文本(如&eacute;)。

Reference

你可以看看一些插件或創建自己的插件,這將基本上創建一個下拉窗口小部件與div的或其他一些標記出你提供選擇的選項。

+0

我已經意識到它是無效的HTML。我試圖找到一種語法上有效的方法將HTML元素嵌入到下拉列表中。 – 2013-05-10 20:54:44

+0

在這一點上沒有聯繫的方式。你將不得不創建一個小部件。或者使用一些插件來創建您提供的選擇中的下拉小部件。 – PSL 2013-05-10 20:55:40

+2

@AndersonGreen答案正是這樣說的:*沒有語義上有效的方法來做到這一點。 – Adrian 2013-05-10 21:00:49

0

不,你不能。如果你想要,你可以使用CSS來創建你想要的。但這並不容易。

+0

您可以在miniclip.com的搜索框中找到示例。 – 2013-05-10 20:56:58

+0

你知道如何使用CSS來做到這一點嗎? – 2013-05-10 21:19:25

+0

不,但我會嘗試學習,如果你想,我會告訴你明天。 – 2013-05-10 21:23:04

相關問題