gpt4 book ai didi

jquery - 防止文本超出包含的

转载 作者:行者123 更新时间:2023-11-28 02:57:52 24 4
gpt4 key购买 nike

我有这个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/

24 4 0
文章推荐: javascript - JQUERY 表单不提交
文章推荐: html - 在 CSS 中将垂直 与居中定位的 对齐