javascript - CSS 高亮选中的按钮
问题描述
我正在尝试对应用程序引入的代码进行样式设置,因此我无法调整 HTML。
我的目标是将单选按钮输入变成隐藏单选按钮的可点击按钮。到目前为止,除了突出显示选定的按钮之外,我已经完成了这项工作。当单选按钮被选中时,我似乎无法弄清楚如何定位标签,因为它是输入的父级。
我试过用 CSS 和 jQuery 来定位标签,但都没有奏效。
$(document).ready(function() {
$(".bold_options label").click(function() {
$(".bold_options label").removeClass("selected");
$(this).addClass("selected")
})
})
.bold_options input[type="radio"] {
display: none;
}
.bold_options label {
display: inline-block;
float: left;
min-width: 36px;
height: $sw-height;
/* No extra spacing between them */
margin: 0;
/* Styling text */
font-size: 13px;
text-align: center;
line-height: $sw-height;
white-space: nowrap;
text-transform: uppercase;
opacity: 0.8;
display: flex;
justify-content: center;
align-items: center;
font-weight: normal;
border: 2px solid #C0C2BF !important;
background-color: {
{
settings.color_swatches_btn
}
}
;
color: {
{
settings.color_swatches_text
}
}
;
background-position: center;
padding: 0 10px;
}
.selected {
outline-color: transparent;
border: 2px solid #F9DDD2 !important;
font-weight: 600;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="bold_option_value ">
<label>
<span class="bold_option_value_element">
<input type="radio" class="rb_905341_375591" name="properties[EDGES]" value="DECKLE HAND-TORN EDGES" data-option_value_key="0">
</span>
<span class="bold_option_value_title">DECKLE HAND-TORN EDGES</span>
</label>
</span>
单击按钮时,边框颜色应更改以突出显示它是选定的按钮。
解决方案
基于此:我的目标是将单选按钮输入变成一个可点击的按钮,并隐藏单选选择选项。,我认为这就是你所追求的:
$(document).ready(function() {
$(".bold_option_value_element").parent().hide();
$(".bold_option_value").append('<button class="something">DECKLE HAND-TORN EDGES</button>');
$(".something").click(function() {
alert('You clicked the new button');
})
})
.bold_options input[type="radio"] {
display: none;
}
.bold_options label {
display: inline-block;
float: left;
min-width: 36px;
height: $sw-height;
/* No extra spacing between them */
margin: 0;
/* Styling text */
font-size: 13px;
text-align: center;
line-height: $sw-height;
white-space: nowrap;
text-transform: uppercase;
opacity: 0.8;
display: flex;
justify-content: center;
align-items: center;
font-weight: normal;
border: 2px solid #C0C2BF !important;
background-color:
background-position: center;
padding: 0 10px;
}
.selected {
outline-color: transparent;
border: 2px solid #F9DDD2 !important;
font-weight: 600;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="bold_option_value ">
<label>
<span class="bold_option_value_element">
<input type="radio" class="rb_905341_375591" name="properties[EDGES]" value="DECKLE HAND-TORN EDGES" data-option_value_key="0">
</span>
<span class="bold_option_value_title">DECKLE HAND-TORN EDGES</span>
</label>
</span>
推荐阅读
- r - 如何在R中为3个自变量和1个因变量绘制线图
- amazon-web-services - CDK - 如何在构造中获取堆栈名称
- android - MATCH_CONSTRAINT 的含义是什么?
- sql - 在同一个表中加入 ID 上的事务
- python - 如何从用于在 tensorflow 服务中加载多个模型的自定义模型配置文件中删除模型详细信息?
- docker - NGINX 反向代理不适用于在 Docker 中运行的 .NET 核心 webAPI
- sql-server - 删除列后使用实体框架将数据保存到 sql server 时出错
- python - 如何使用子进程运行这个命令行语句?
- android - Flutter 可重用组件
- javascript - 使用 Javascript/ajax 传递时凭据无效