Jquery adding a class to div with no class name
I have the following markup:
<body id="terms_and_conditions_page" dir="ltr">
<div class="main-wrapper"></div> <---------* Edit
开发者_运维知识库 <div> <-------*
<div class="page_banner describes_page_content body-bg"></div>
<div class="content-bg"></div>
</div>
</body>
and I am trying to add a class (tc-content) to a div with no class name so the markup will look like:
<body id="terms_and_conditions_page" dir="ltr">
<div class="main-wrapper">
<div class="tc-content"> <-------*
<div class="page_banner describes_page_content body-bg"></div>
<div class="content-bg"></div>
</div>
</body>
I tried $('#terms_and_conditions_page:nth-child(2)').addClass('tc-content');
but it adds a class to every child of this empty div.
Is there an easy way to do it ?
Edit: sorry guy, I copied that from firebug so it skipped the closing div. See the edit
There are many :
1. #terms_and_conditions_page>*:nth-child(2)
2. $('.main-wrapper').next()
3. $('.page_banner').parent()
try this:
$('.main-wrapper').next().addClass('tc-content');
use :not
here is the fiddle http://jsfiddle.net/r89Sa/1/
Is it just one div? Assign an id to it, and then you can get the element by id and add the class to it.
$("#terms_and_conditions_page div:not([class])").attr("class","tc-content")
Will work
It seems like .main-wrapper
should be the parent div? Try:
$(".main-wrapper > div").eq(0).addClass("tc-content");
$('#terms_and_conditions_page > div > div:first').addClass('tc-content');
The div you want to select is the child of a child of the body tag, not a direct child of the body tag. With the exact markup you don't really need the ':first' that I put in the selector since there is only one div in that tier.
...which means there is an un-closed div tag in your markup:
<body id="terms_and_conditions_page" dir="ltr">
<div class="main-wrapper">
<div> <-------*
<div class="page_banner describes_page_content body-bg"></div>
<div class="content-bg"></div>
</div>
</body>
Should be:
<body id="terms_and_conditions_page" dir="ltr">
<div class="main-wrapper">
<div> <-------*
<div class="page_banner describes_page_content body-bg"></div>
<div class="content-bg"></div>
</div>
</div>
</body>
Try this
$("div.page_banner").parent().addClass("tc-content");
精彩评论