jquery - 防止文本超出包含的 <div>

标签 jquery html css overflow

我有这个styled-select这是我根据我的要求创建的。当其中一个选项的长度超过包含 <div> 的大小时我选择它,它会转到下一行。我希望它被隐藏并且不可滚动。我用过 overflow(-x):hidden但它没有用。

这是工作 jsFiddle

 $(function() {

   $('.styled-select select').hide();
   $("select#elem").val('0');

   $('.styled-select div').each(function() {
     var $container = $(this).closest('.styled-select');
     $(this).html($container.find('select option:selected').text());
   });

   $('.styled-select div').click(function() {
     var $container = $(this).closest('.styled-select');
     var opLen = $container.find('select').children('option').length;
     if (opLen < 5) {
       $container.find('select').show().attr('size', opLen).focus();
     } else {
       $container.find('select').show().attr('size', 5).focus();
     }
   });

   $('.styled-select select').click(function() {
     var $container = $(this).closest('.styled-select');
     var text = $container.find('select option:selected').text();
     $container.find('div').html(text);
     $container.find('select').hide();
   });

   $('.styled-select select').focusout(function() {
     var $container = $(this).closest('.styled-select');
     $container.find('select').hide();
   });

 });
	.styled-select select {
	  position: absolute;
	  background: transparent;
	  width: 420px;
	  padding-top: 5px;
	  font-size: 18px;
	  font-family: 'PT Sans', sans-serif;
	  color: black;
	  border: 0;
	  border-radius: 4;
	  -webkit-appearance: none;
	  -moz-appearance: none;
	  -o-appearance: none;
	  z-index: 1;
	  outline: none;
	  top: 42px;
	  box-shadow: 0 2px 2px 0 #C2C2C2;
	}
	
	.styled-select {
	  background: url('../img/campaignSelector.png') no-repeat right;
	  background-color: white;
	  width: 420px;
	  height: 42px;
	  position: relative;
	  margin: 0 auto;
	  box-shadow: 0 2px 2px 0 #C2C2C2;
	  background-position: 97% 50%;
	}
	
	.styled-select option {
	  font-size: 18px;
	  background-color: white;
	  margin-left: 3px;
	}
<div class="styled-select" style="width: 301px; margin:5px 0 0 10px;">
  <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="https://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
  <div id="userChannelDivId" style="font-size:18px; position: absolute; top: 8px; left: 5px; width: 300px; height: 42px;"></div>
  <select id="userChannelId" name="userChannelId" style="width:100%">
    <option value="">resh resh resh resh resh resh resh resh resh resh</option>
    <option value="">--- Belect ---</option>
    <option value="">--- Celect ---</option>
    <option value="">--- Delect ---</option>
    <option value="">--- Felect ---</option>
    <option value="">--- Gelect ---</option>
  </select>
</div>

最佳答案

您需要在 styled-select 类本身上使用 overflow: hidden;。我还将容器的高度降低到 35px,这样第二条隐藏线就看不到了。请参阅下面的代码。

另一种可能性是将white-space: nowrap 添加到类styled-select 的样式属性中。这样文本就不会在 div 内换行。

 $(function() {

   $('.styled-select select').hide();
   $("select#elem").val('0');

   $('.styled-select div').each(function() {
     var $container = $(this).closest('.styled-select');
     $(this).html($container.find('select option:selected').text());
   });

   $('.styled-select div').click(function() {
     $('.styled-select').css('overflow', 'visible');
     var $container = $(this).closest('.styled-select');
     var opLen = $container.find('select').children('option').length;
     if (opLen < 5) {
       $container.find('select').show().attr('size', opLen).focus();
     } else {
       $container.find('select').show().attr('size', 5).focus();
     }
   });

   $('.styled-select select').click(function() {
     $('.styled-select').css('overflow', 'hidden');
     var $container = $(this).closest('.styled-select');
     var text = $container.find('select option:selected').text();
     $container.find('div').html(text);
     $container.find('select').hide();
   });

   $('.styled-select select').focusout(function() {
     $('.styled-select').css('overflow', 'hidden');
     var $container = $(this).closest('.styled-select');
     $container.find('select').hide();
   });

 });
	.styled-select select {
	  position: absolute;
	  background: transparent;
	  width: 420px;
	  padding-top: 5px;
	  font-size: 18px;
	  font-family: 'PT Sans', sans-serif;
	  color: black;
	  border: 0;
	  border-radius: 4;
	  -webkit-appearance: none;
	  -moz-appearance: none;
	  -o-appearance: none;
	  z-index: 1;
	  outline: none;
	  top: 42px;
	  box-shadow: 0 2px 2px 0 #C2C2C2;
	}
	
	.styled-select {
	  background: url('../img/campaignSelector.png') no-repeat right;
	  background-color: white;
	  width: 420px;
	  height: 35px;
	  position: relative;
	  margin: 0 auto;
	  box-shadow: 0 2px 2px 0 #C2C2C2;
	  background-position: 97% 50%;
	}
	
	.styled-select option {
	  font-size: 18px;
	  background-color: white;
	  margin-left: 3px;
	}
<div class="styled-select" style="width: 301px; margin:5px 0 0 10px; overflow: hidden;">
  <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="https://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
  <div id="userChannelDivId" style="font-size:18px; position: absolute; top: 8px; left: 5px; width: 300px; height: 42px;"></div>
  <select id="userChannelId" name="userChannelId" style="width:100%">
    <option value="">resh resh resh resh resh resh resh resh resh resh</option>
    <option value="">--- Belect ---</option>
    <option value="">--- Celect ---</option>
    <option value="">--- Delect ---</option>
    <option value="">--- Felect ---</option>
    <option value="">--- Gelect ---</option>
  </select>
</div>

关于jquery - 防止文本超出包含的 <div>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36130682/

相关文章:

html - Angular :L 形边框

html - 在另一个 div 中左右定位 div

css - 如何设置 webkit-transform-origin 在其边缘滚动 3D 立方体?

javascript - Django for 循环列表与 jquery 和 javascript

html - 内部 div 和节或文章等语义元素之间有什么区别?

javascript - 希望 JavaScript 脚本在 Div 位于视口(viewport)中时运行

html - 输入宽度 100% 和边距/填充

html - 为什么图像下拉列表在某些浏览器上不显示图像?

jquery - 移动模拟器工作吗?iPad 上的 Css Jquery

jquery - 更改选择菜单的选中框中的文本 onChange 事件