gpt4 book ai didi

javascript - 限制 div 中的文本,但使整个 div 可点击

转载 作者:太空宇宙 更新时间:2023-11-03 23:01:58 26 4
gpt4 key购买 nike

我有这个 styled-select 是我根据自己的要求制作的。现在的问题是,如果选中的选项大于整个区域,自然会填满整个div,如下图所示:

enter image description here

现在我希望它被限制或者右边有一个边距,以便显示我用作background: url的箭头,如下图:

enter image description here

如果我减小 div 的宽度就可以了,但问题是它无法点击以打开下拉菜单。现在我想知道是否有解决方案来限制文本但仍然有可点击区域?

这是我的代码: Working 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('http://s22.postimg.org/f5gjjtswd/campaign_Selector.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: 295px; height: 42px; white-space:nowrap; overflow:hidden"></div>
<select id="userChannelId" name="userChannelId" style="width:100%">
<option value="">resh resh reshadreshadreshadreshadreshad resh</option>
<option value="">--- Select ---</option>
<option value="">--- Celect ---</option>
<option value="">--- Delect ---</option>
<option value="">--- Felect ---</option>
<option value="">--- Gelect ---</option>
</select>
</div>

最佳答案

更改为 #UserChannelDivID...

width: 265px;
text-overflow: ellipsis;
padding: 0 30px 0 0;

Fiddle

关于javascript - 限制 div 中的文本,但使整个 div 可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36160280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com