Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可选<;optgroup>;在HTML中<;选择>;标签_Html_Html Select_Optgroup - Fatal编程技术网

可选<;optgroup>;在HTML中<;选择>;标签

可选<;optgroup>;在HTML中<;选择>;标签,html,html-select,optgroup,Html,Html Select,Optgroup,有没有办法使选项组可选择 子标签 子标签 问题不是很清楚,但这就是您要寻找的: <select multiple="multiple"> <optgroup label="Parent Tag"> <option value="1">Child Tag</option> <option value="2">Child Tag</option> </optgroup> <

有没有办法使选项组可选择


子标签
子标签

问题不是很清楚,但这就是您要寻找的:

<select multiple="multiple">
   <optgroup label="Parent Tag">
      <option value="1">Child Tag</option>
      <option value="2">Child Tag</option>
   </optgroup>
</select>

子标签
子标签

我不认为你可以,但是你可以很容易地用css复制视觉样式,因此在你的选择中只有选项,所以一切都是可选择的

.optionGroup{
字体大小:粗体;
字体:斜体;
}
.选择儿童{
左侧填充:15px;
}

父标签
子标签
子标签

@grifos的答案在WebKit浏览器中不受支持,在IE 11中测试时不起作用

一个建议可能是使用无序/有序列表并使用CSS对其进行样式化,然后使用JavaScript/jQuery添加功能


我在过去看到过一个很好的实现,它看起来非常光滑

这在普通html中是不可能的。少数浏览器(mozilla)允许您使用css实现类似的功能,但在撰写本文时,大多数浏览器(webkit等)不支持html选择元素的样式

但是,有许多javascript库旨在增强html选择小部件,并提供缺少的功能,如您请求的功能。举几个例子:


这将给人一种带有可选optgroups的下拉列表的感觉。让它非常简单,但你可以风格你的心内容

.hide {
  display:none;
}
.show {
  display:block;
}
.selected.button {
  display:block;
  font-weight: bold;
}
button {
  text-align: left;
  min-width: 200px;
  margin-left: 10px;
  border: 0px;
  background: #eee;
  display: block;
}
#value_display {
  width: 210px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding-left: 8px;
}
</style>
<html>
<body>


<!-- Div is a display and also trigger for displaying dropdown -->
<div id="value_display" onclick="showOptions(this.value)">
  opt 0
</div>

<!-- setting height and doing overflow-y allows dropdown list with scrollbox -->
<div id="select_div" class="hide" style="height: 80px; overflow-y: scroll;">

<button class="selected button" onclick="select(this.id)" id="opt 0">opt 0</button>
<button onclick="select(this.id)" id="opt 0-1" style="padding-left: 15px">opt 0-1</button>
<button onclick="select(this.id)" id="opt 0-2" style="padding-left: 15px">opt 0-2</button>
<button onclick="select(this.id)" id="opt 0-3" style="padding-left: 15px">opt 0-3</button>
<button onclick="select(this.id)" id="opt 0-4" style="padding-left: 15px">opt 0-4</button>
<button class="" onclick="select(this.id)" id="opt 1">opt 1</button>
<button onclick="select(this.id)" id="opt 1-1" style="padding-left: 15px">opt 1-1</button>
<button onclick="select(this.id)" id="opt 1-2" style="padding-left: 15px">opt 1-2</button>
<button onclick="select(this.id)" id="opt 1-3" style="padding-left: 15px">opt 1-3</button>
<button onclick="select(this.id)" id="opt 1-4" style="padding-left: 15px">opt 1-4</button>
.hide{
显示:无;
}
.表演{
显示:块;
}
.selected.button{
显示:块;
字体大小:粗体;
}
钮扣{
文本对齐:左对齐;
最小宽度:200px;
左边距:10px;
边界:0px;
背景:#eee;
显示:块;
}
#数值显示{
宽度:210px;
边框:1px实心#ddd;
边界半径:4px;
左侧填充:8px;
}
选择0
选择0
选择0-1
选择0-2
选择0-3
选择0-4
选择1
选择1-1
选择1-2
选择1-3
选择1-4


var showingOptions=false;
var showingID=document.getElementById('value\u display').innerHTML;
函数showOptions(){
如果(显示选项){
document.getElementById('select_div').className=“hide”;
显示选项=假;
}
否则{
document.getElementById('select_div').className=“show”;
showingOptions=true;
}
}
功能选择(id){
document.getElementById(id).className='selected button';
document.getElementById(showingID).className='show';
showingID=id;
document.getElementById('value_display')。innerHTML=id;
document.getElementById('select_div')。className='hide';
}

一个稍微不同的解决方案

.optionGroup{
字体大小:粗体;
}

父标签
子标签1
子标签2

在html标准支持这一点之前,给出的任何和所有答案都是有问题的,包括:

  • 允许在select的子级上使用类属性,但样式应用于元素的条件/方式在浏览器和浏览器版本之间有很大差异
  • 前缀为的结果是,如果select元素比所选选项窄,视觉效果将不令人满意,可读文本隐藏在右侧(请参见下面的示例)
  • 应用的任何样式都不一定适合浏览器用户已经习惯的样式。粗体和斜体是Firefox,但不一定是所有浏览器。许多浏览器应用包括灰色在内的样式,以帮助指示optgroup不可选择
  • 用户不一定会想到select optgroup标签的概念 这使我得出结论,解决此问题的最佳方法是使用库式UI,可选择整个站点的所有选择(为了一致性),或者使用optgroup中的第一个选项来表示选择所有子项,并提供清晰的描述(例如“所有瑞典车”):

    
    父母亲
    小孩
    
    很抱歉没有讲清楚。我希望optgroup的选择方式与您选择选项的方式相同Orry Forget option Styleing是一个混乱的网页浏览器至少您可以通过添加最漂亮的实现来创建缩进,但它会产生所需的结果。:-)粗体在FF中不适用。From:“此外,
    元素不能被选择。这是HTML规范的限制,不是Select2可以克服的限制。”除非你想沿着库的路线走,否则这是到目前为止我最喜欢的解决方案。谢谢很好的信息,但是关于哪一条是最佳路线的结论很大程度上取决于开发人员所追求的:他们针对的是哪些浏览器,他们是尝试重新创建
    optgroup
    还是简单地签出项目,以及用例的其他各个方面。
    <script>
    
    var showingOptions = false;
    var showingID = document.getElementById('value_display').innerHTML;
    
    function showOptions() {
     if(showingOptions) {
       document.getElementById('select_div').className = "hide";
       showingOptions = false;
     }
     else {
       document.getElementById('select_div').className = "show";
       showingOptions = true;
     }
    }
    function select(id){
      document.getElementById(id).className = 'selected button';
      document.getElementById(showingID).className = 'show';
      showingID = id;
      document.getElementById('value_display').innerHTML = id;
      document.getElementById('select_div').className = 'hide';
    }
    
    </script>
    
    <select multiple="multiple">
       <optgroup label="Parent">
          <option value="0" class="optionChild">ALL Children</option>
          <option value="1" class="optionChild">Child Tag 1</option>
          <option value="2" class="optionChild">Child Tag 2</option>
       </optgroup>
    </select>