简体   繁体   English

下拉选择包含div单击纯javascript

[英]dropdown select on containing div click pure javascript

<div class="container">
  <select class="form_select">
    <option>the option</option>
  </select>
</div>

<style>
  .container {
    width: 300px;
    border:1px solid blue;
  }
  select {
    max-width:50px;
  }
</style>

I have a div containing a select option. 我有一个包含选择选项的div。 The div is larger than the select box. div大于选择框。 Using a pure javascript solution, I would like the select dropdown to happen when the containing div is clicked outside the select box area. 使用纯JavaScript解决方案,我希望在选择框区域外单击包含div时发生选择下拉列表。 The only solutions I have found are jquery and I am seeking a pure javascript solution. 我找到的唯一解决方案是jquery,我正在寻求一个纯粹的JavaScript解决方案。

I don't need exact code (but it helps) as I am not very good with javascript so please explain any answer completely. 我不需要确切的代码(但它有帮助),因为我对javascript不太好,所以请完整解释任何答案。

https://jsfiddle.net/m6s5j9sj/1/ https://jsfiddle.net/m6s5j9sj/1/

There may not be a simple way but you can try to set the size & length . 可能没有一种简单的方法,但您可以尝试设置sizelength The catch is it will only work if length is greater than 1. So in the demo I have created another option 'Select'.Hopefully in your application there will be multiple option 问题是它只有在长度大于1时才能工作。所以在演示中我创建了另一个选项'Select'。在你的应用程序中有多个选项

 document.getElementById('container').addEventListener('click', function(e) { var seleOpt = document.getElementsByClassName('form_select')[0]; seleOpt.size = seleOpt.options.length; }) 
 .container { width: 300px; border: 1px solid blue; } select { max-width: 50px; } 
 <div class="container" id="container"> <select class="form_select"> <option>Select</option> <option>the option</option> </select> </div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM