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的类?
答案 0 :(得分:1)
$('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;
答案 1 :(得分:1)
使用它:
$('header').siblings('section:first').css('background', '#e4e4e4');
答案 2 :(得分:1)
尝试以下代码
<!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;