JQuery different methods for adding classes
I have seen a few different methods for adding classes to dynamically creat开发者_开发问答ed elements using JQuery.
I'm most familiar with
$("<div />").addClass("class1 class2");
however I have seen a lot of
$("<div />", {
class : "class1 class2"
});
When I test out the second method in a Fiddle I can see both class1 and class2 are applied.
however, when applied to what i'm working on
// this does not work
var b = $("<div id='tweetBox' />", {
class : "triangle-right right"
});
// this works
var b = $("<div id='tweetBox' />").addClass("triangle-right right");
you can't mix and match.
try:
var b = $("<div />", {
id: "tweetBox",
class : "triangle-right right"
});
// this does not work
var b = $("<div id='tweetBox' />", {
class : "triangle-right right"
});
Doesn't work because there is an attribute on the element you are creating.
This will work
var b = $("<div />", {
id: 'tweetBox',
class : "triangle-right right"
});
精彩评论