防止文本超出包含<div>

时间:2016-03-21 12:11:39

标签: jquery html css overflow

由于我的要求,我创建了styled-select。当其中一个选项长于包含<div>的大小并选择它时,它将转到下一行。我希望它隐藏起来而不是可滚动的。我使用了overflow(-x):hidden,但它没有用。

这是工作jsFiddle

&#13;
&#13;
 $(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();
   });

 });
&#13;
	.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;
	}
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要在课程overflow: hidden;上使用styled-select。我还将容器的高度减小到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>