使用jQuery在特定div之后的第一部分中添加一个类

时间:2015-08-28 12:47:56

标签: jquery

HTML:

<header id="header" class="">
    <div class="header-top-row">

Some fancy code.        

    </div>
</header>

<style>
Some fancy styles.
</style>

<section>
</section>

<section>
</section>

<section>
</section>

如何在标题后面的第一部分添加带有jquery的类?

3 个答案:

答案 0 :(得分:1)

&#13;
&#13;
$('header ~ section:first').addClass('test');
&#13;
.test{
  color:green;
  }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<header id="header" class="">
    <div class="header-top-row">

Some fancy code.        

    </div>
</header>

<style>
Some fancy styles.
</style>

<section>
  section
</section>

<section>
  section
</section>

<section>
   section
</section>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用它:

$('header').siblings('section:first').css('background', '#e4e4e4');

答案 2 :(得分:1)

尝试以下代码

&#13;
&#13;
<!doctype html>
<html>
<head>
<title>prepend demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$('document').ready(function(){
	$( "body section" ).first().addClass('test');
})
</script>

</style>

</head>
<body>
<section>
</section>
<section>
</section>

</body>
&#13;
&#13;
&#13;