HTML Drowdown显示图片

时间:2016-12-11 16:05:04

标签: html css

如果我选择的文字对于我为下拉列表设置的宽度太长,我需要在下拉图片区域中不显示所选文本。我认为text-overflow: clip;中的overflow: hiddenspan.selectInner会起作用,但事实并非如此。

enter image description here

span.selectInner随数据一起添加。以下是添加该类的片段:

JS

$('.filterSelectWrap').append('<span class="selectInner">' + filter + '</span>');

HTML

<div class="selectWrapper">
    <label class="selectLabel">Filter:</label>
    <div class="innerSelectWrapper">
        <div class="filterSelectWrap">
            <select id="filterComboBox"></select>
        </div>
    </div>
</div>

CSS

.selectWrapper {
    float: left; 
    margin: 0; 
    padding: 5px;
}

.innerSelectWrapper {
    float: left; 
    width: 150px; 
    margin-left: 5px;
}

span.selectInner {
    position: absolute;
    display: block;
    margin: 0;
    padding: 0 0 0 5px;
    width: 100%;
    height: 22px; /* set same height */
    line-height: 22px; /* set same height */
    color: #333;
    white-space: nowrap;
    overflow: hidden;
    text-align: left;
    text-overflow: clip;
    font-weight: normal;
    font-size: 1em;
    z-index: 901;
    background: url('../images/arrow.png') no-repeat 100% -5px #fff; /* fallback bg image*/
    background: url('../images/arrow.png') no-repeat 100% -5px, -webkit-linear-gradient(top, #fff, #9c9c9c);
    background: url('../images/arrow.png') no-repeat 100% -5px, -moz-linear-gradient(top, #fff, #9c9c9c);
    background: url('../images/arrow.png') no-repeat 100% -5px, -ms-linear-gradient(top, #fff, #9c9c9c);
    background: url('../images/arrow.png') no-repeat 100% -5px, -o-linear-gradient(top, #fff, #9c9c9c);
    background: url('../images/arrow.png') no-repeat 100% -5px, linear-gradient(top, #fff, #9c9c9c);
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    -ms-border-radius: 4px;
    -o-border-radius: 4px;
    border-radius: 4px;
    -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    -moz-box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    -ms-box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    -o-box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

select {
    width: 100%;
    height: 22px; /* set same height */
    position: absolute;
    left: 0;
    z-index: 902;
    padding: 0;
    margin: 0;
    border: 0 none;
    float: none;
    opacity: 0;
    cursor: pointer;
    -webkit-appearance: menulist-button;
}

3 个答案:

答案 0 :(得分:0)

你有没有考虑过这样的事情......

text-overflow: clip;
text-overflow: ellipsis;
text-overflow: "…";

在填充和使用方面看看这个。

&#13;
&#13;
p {
    width: 200px;
    border: 1px solid;
    padding: 2px 25px 2px 5px;

    /* BOTH of the following are required for text-overflow */
    white-space: nowrap;
    overflow: hidden;
}

.overflow-ellipsis {
    text-overflow: ellipsis;
}

.overflow-string {
    /* Not supported in most browsers, 
       see the 'Browser compatibility' section below */
    text-overflow: " [.]"; 
}
&#13;
<p class="overflow-ellipsis">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

将填充权限给文本容器

右边的填充值应大于或等于该箭头图像的宽度。

并执行text-overflow : ellipsis

问题是文本在填充框中被剪切,ellipsis在内容框中进行剪切

这可能对您有所帮助Prevent text from overflowing a padded container in HTML

答案 2 :(得分:0)

你应该将内部文本filter附加到另一个子div(在我的例子中命名为.inner-text)中,例如:

$('.filterSelectWrap').append('<span class="selectInner"><span class="inner-text">' + filter + '</span></span>');

并将这些CSS规则提供给.inner-text

.inner-text {
  display: block;
  padding-right: 20px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

查看下面的工作代码段:

&#13;
&#13;
var filter = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates quaerat dolore iusto ducimus adipisci natus dicta, veritatis pariatur magni quos error explicabo provident iure velit dolorem odio! Sed, ratione sit.';

$('.filterSelectWrap').append('<span class="selectInner"><span class="inner-text">' + filter + '</span></span>');
&#13;
.selectWrapper {
    display: flex;
    margin: 0; 
    padding: 5px;
}

.innerSelectWrapper {
    display: inline-block;
    width: 150px;
    margin-left: 5px;
    position: relative;
}

span.selectInner {
    position: absolute;
    display: block;
    margin: 0;
    padding: 0 0 0 5px;
    width: 100%;
    height: 22px; /* set same height */
    line-height: 22px; /* set same height */
    color: #333;
    font-size: 1em;
    z-index: 901;
    background: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-down-b-128.png') no-repeat 100%, -webkit-linear-gradient(top, #fff, #9c9c9c);
    background: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-down-b-128.png') no-repeat 100%, -moz-linear-gradient(top, #fff, #9c9c9c);
    background: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-down-b-128.png') no-repeat 100%, -ms-linear-gradient(top, #fff, #9c9c9c);
    background: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-down-b-128.png') no-repeat 100%, -o-linear-gradient(top, #fff, #9c9c9c);
    background: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-down-b-128.png') no-repeat 100%, linear-gradient(top, #fff, #9c9c9c);
    background-size: contain;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    -ms-border-radius: 4px;
    -o-border-radius: 4px;
    border-radius: 4px;
    -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    -moz-box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    -ms-box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    -o-box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

select {
    width: 100%;
    height: 22px; /* set same height */
    position: absolute;
    left: 0;
    z-index: 902;
    padding: 0;
    margin: 0;
    border: 0 none;
    float: none;
    opacity: 0;
    cursor: pointer;
    -webkit-appearance: menulist-button;
}

.inner-text {
  display: block;
  padding-right: 20px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="selectWrapper">
    <label class="selectLabel">Filter:</label>
    <div class="innerSelectWrapper">
        <div class="filterSelectWrap">
            <select id="filterComboBox"></select>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

希望这是你想要实现的目标。它有帮助!