Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery命令没有给出期望的结果_Jquery_Html_Css - Fatal编程技术网

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

我希望使用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
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') 

}