javascript - 选择的默认样式

标签 javascript jquery html css

<select id="select">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

<span id="red">red </span> <span id="default">default</span>

$("#red").click(function(){
    $("#select").css('background-color', 'red');
});

$("#default").click(function(){
    $("#select").css('background-color', 'white');
});

如果我点击红色然后选择是红色的。我如何为选择设置默认样式,反对白色?

最佳答案

只需删除该属性即可将背景色重置为其默认值:

$("#default").click(function(){
    $("#select").css('background-color', '');
});

有关引用,请参阅 jQuery .css() docs .引用:

Setting the value of a style property to an empty string — e.g. $('#mydiv').css('color', '') — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property.

关于javascript - 选择的默认样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7077153/

相关文章:

javascript - Webpack 动态加载代码拆分包

jquery - 使用 jquery 对选定的复选框进行计数。

javascript - 将多行 PHP 字符串插入 Javascript 变量

javascript - 切换功能使所有其他 div 消失

html - 重新订购 asp :GridView? 的简单方法

html - 多行文本值破坏布局的水平对齐

javascript - 如何在 onclick 事件期间更改 href

javascript - 自动打开选择框

html - 是否无法在 Bootstrap 5 中更改 <body> 的字体系列?

javascript - 当有新请求时取消正在进行的 Angular $http 请求