开发者

Accessing a div with Class using jquery

I have a div with Class. For example

<div class="button check"></div>.The css is defined for both 'button','check'. I want to access the开发者_开发技巧 above div through jquery and write something in the div.I tried with

$('.button check').html("sample data");

I do not see anything being written when I run the page.

Please help me.


Just chain the CSS classes together, separated by periods:

$('.button.check').html("sample data");

You will also see better performance in some browsers, by specifying the tag name as well:

$('div.button.check').html("sample data");

Update: After reading Brian's answer, I re-read the original question I realized you might be under the impression you need to use both to reference the div. If you had:

<div class="button cancel">Cancel</div>
<div class="button check">Check</div>

Then chaining selectors (i.e. .button.cancel) would make sense. However, if it was the only div on the page or you wanted all the divs with button class, you don't need both classes:

<div class="button check">Check</div>

This would select it fine:

$('div.check').html("sample data");


Can you just target one class, do you need both? You can do one class like:

$("div.button")

OR

$(".button")

For multiple classes, it could be:

$(".button, .check")

OR:

$(".button").add(".check")

HTH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜