jquery - 检查属性时如何将类添加到 ID?
问题描述
我正在研究一个也在此处描述的解决方案。
现在我很接近了,我想:
jQuery("#first-slide[data-isactiveslide="true"]").addClass("underline");
但我需要将类添加到我的 ID(我的按钮)。我该怎么做呢?我是这样想的,但它不起作用:
jQuery("#first-slide[data-isactiveslide="true"]"){
jQuery("#my-button").addClass("underline");
});
任何帮助表示赞赏最好的问候,Skt
解决方案
您可以将选择器包装jQuery
在 aif
中以检查它是否找到任何东西,如果找到,请将类添加到按钮。
if (jQuery("#first-slide[data-isactiveslide='true']").length) {
jQuery("#my-button").addClass("underline");
}
.underline { text-decoration: underline; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='first-slide' data-isactiveslide='true'>#1</div>
<div id='second-slide' data-isactiveslide='false'>#2</div>
<button id='my-button'>Button</button>
编辑:相同的代码,isactiveslide=false
显示按钮没有下划线
if (jQuery("#first-slide[data-isactiveslide='true']").length) {
jQuery("#my-button").addClass("underline");
}
.underline { text-decoration: underline; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='first-slide' data-isactiveslide='false'>#1</div>
<button id='my-button'>Button</button>
推荐阅读
- javascript - 在JS中将数组语句的第一个字母大写
- javascript - 未捕获的语法错误:导出和导入类时无法在模块外部使用导入语句
- c++ - 关于在嵌入式系统上使用 std::filesystem 的建议
- linux - 如何使用 bash 脚本替换文件中某行的某些内容?
- python - 我在连接我正在制作的书店 gui 的前端和后端时遇到问题
- javascript - 简单的 Javascript 将数字从 1 到 11 转换成单词?
- python - 具有多个条件的 pyarrow 数据集过滤
- flutter - 如何从 Bottomnavigationbar 类访问 tabcontroler 到 tabBar_view 类?
- c - 数组写入时出现分段错误
- android - 如何只触发 onPrepareOptionsMenu 而不是 onCreateOptionsMenu?