jQuery命令没有给出期望的结果
我希望使用html jquery将jQuery命令没有给出期望的结果,jquery,html,css,Jquery,Html,Css,我希望使用html jquery将active添加到最后一个img元素的类名中 我在HTML和jquery中有一些元素,如下所示: 函数initialSteps(){ $(“.row”).find(“column:last”).find(“img:first”).className+=“active”; //这也不行 //$(“.row”).find(“column:last”).find(“演示光标:last”).className+=“活动”; } 尝试.column而不是column和a
active
添加到最后一个img元素的类名中
我在HTML和jquery中有一些元素,如下所示:
函数initialSteps(){
$(“.row”).find(“column:last”).find(“img:first”).className+=“active”;
//这也不行
//$(“.row”).find(“column:last”).find(“演示光标:last”).className+=“活动”;
}
尝试
.column
而不是column
和addClass()
$(.row”).find(.column:last”).find(“img:first”).addClass(“active”)
.active{
边框:3倍纯红;
}
你能试试下面的代码吗
<!DOCTYPE html>
<html>
<head>
<title>Checked Boxes</title>
<script src="https://code.jquery.com/jquery-1.10.2.js" ></script>
</head>
<body>
<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>
</body>
<script>
jQuery(document).ready(function($) {
$( "div.row .column:last-child" ).find("img:first").addClass("active");
});
</script>
</html>
复选框
jQuery(文档).ready(函数($){
$(“div.row.column:last child”).find(“img:first”).addClass(“active”);
});
你做得不对。不能使用“+”运算符为类追加值
jQuery具有在任何元素中添加新类的内置方法。
您可以使用下面的函数代替您的函数
您可以做得更简单:
function initialSteps() {
$(".column:last-child img:first-child").addClass("active");
}
您需要利用jQuery中的函数:
function initialSteps() {
$(".row").find(".column:last").find("img:first").addClass("active");
}
您可以通过如下链接选择器来缩短该时间:
function initialSteps() {
$(".row .column:last img:first").addClass("active");
}
您不需要连接太多的
查找
,只需添加一次完整的选择器,这样会更有效
function initialSteps() {
$(".row .column:last img").addClass('active')
}
我回答了你,无论如何我想在这里告诉你的错误。第一个是
column
是一个类,因此需要在选择器之前添加一个点。column:last
。另一个错误是,jquery返回一个包装器对象,而className
是html本机元素,因此您需要获取本机html元素,表示`jqueryobject[0].className+=“active”,它将工作;)
function initialSteps() {
$(".row .column:last img").addClass('active')
}