jquery - jQuery命令没有给出想要的结果
问题描述
我希望active
使用 html jquery 添加到最后一个 img 元素的 className
我在 HTML 和 jquery 中有一些元素,如下所示:
function initialSteps() {
$(".row").find("column:last").find("img:first").className += " active";
// this does not work either
//$(".row").find("column:last").find("demo cursor:last").className += " active";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<div class="column">
<img class="demo cursor" src="img00.jpg">
</div>
<div class="column">
<img class="demo cursor" src="img01.jpg">
</div>
<div class="column">
<img class="demo cursor" src="img02.jpg">
</div>
<div class="column">
<img class="demo cursor" src="img03.jpg">
</div>
<div class="column">
<img class="demo cursor" src="img04.jpg">
</div>
<div class="column">
<img class="demo cursor" src="img05.jpg">
</div>
</div>
但是,它不起作用。有人可以告诉我我做错了什么吗?
解决方案
你做错了。您不能使用“+”运算符为类附加值。
jQuery 具有在任何元素中添加新类的内置方法。
您可以使用以下功能代替您的功能。
function initialSteps() {
$(".row").find(".column:last").find("img:first").addClass("active");
});
推荐阅读
- python - 生成与趋势相关的数据
- javascript - 通过嵌套数组和传递的键匹配进行复杂的 json 排序
- python - ModuleNotFoundError:没有名为“flask_restful”的模块?
- html - SVG 图像徽标未在 Firefox 中显示
- excel - 计算每次变化的列中的数据并在单元格中返回该计数
- java - 如何在具有组合 PK 的表中插入多个带有 JPA 存储库的记录,其中一个是自动递增的?
- database-design - 如何改进这种星型模式设计
- asp.net-mvc - 在 EF MVC 中应用左内连接
- android - 避免用户明确删除应用数据
- javascript - 如何从 CI 构建管道发布 NPM 包并仍然自动化版本控制?