【发布时间】:2019-04-02 14:21:46
【问题描述】:
我正在设计一个表单并且有一些用于自定义选择下拉菜单的 jquery,它的目的是在单击它时将边框颜色更改为黑色,但是当您单击另一个输入字段时,它应该重置为灰色,我有一个用于普通输入字段的切换类设置,但由于选择菜单是高度自定义的,我不得不使用以下代码更改颜色:
$('.select-selected').on('click', function(e){
e.preventDefault();
$(this).css('border', '1px solid black');
});
有没有办法添加到此,以便当您再次单击时,或者最好在.select-selected 之外单击时,它会重置功能并返回灰色边框?感谢任何帮助。
HTML:
<div class="custom-select" type="text" name="enquiry" style="width:100%;" style="max-width:100%;">
<select class="input-select">
<option value="0">Select Enquiry Type</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
</div>
添加了 html 但 .select-selected 被更多的 javascript 添加到 div 中,可以在这里看到我使用的完整代码: https://www.w3schools.com/howto/howto_custom_select.asp
【问题讨论】:
-
必须是jquery吗?听起来 CSS 会更有意义...
.select-selected:focus { border:1px solid black; } -
听起来处理模糊事件可能会有所帮助
-
您的 HTML 没有任何类
select-selected的元素,因此$('.select-selected')将返回一个空集合。 -
添加了 HTML,但是还有更多的 javascript 将 select-selected 添加到 div 这就是为什么你在那里看不到它,是否可以在我提供的 jquery 中添加点击关闭功能? @freefaller 即使使用 '!important',css 也不起作用
-
在
$('.select-selected').on('click',function...)绑定发生之后,您的脚本是否有可能添加了<select>元素?