2015-08-28 2 views
0

HTML:Добавьте класс в первую секцию после определенного DIV с JQuery

<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 в первую секцию после заголовка?

+1

'$ ('header'). Siblings ('section: first')' ..... ?? – Jai

ответ

1

$('header ~ section:first').addClass('test');
.test{ 
 
    color:green; 
 
    }
<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>

1

использовать это:

$('header').siblings('section:first').css('background', '#e4e4e4'); 
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(){ 
 
\t $("body section").first().addClass('test'); 
 
}) 
 
</script> 
 

 
</style> 
 

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

 
</body>

Смежные вопросы