What does this jQuery line mean?
return $('<div>', {
class: "开发者_StackOverflow中文版my_Class"
});
It's returning a newly created <div class="my_Class"></div>
element, this is the
$(html, props)
overload of $()
. It takes the element HTML and an object of properties to set.
Something to note though, class
is a keyword in IE and will cause issues, you need to put it in quotes:
return $('<div>', { 'class': "my_Class" });
A more complete example may be something like:
return $('<div>', { 'class': "my_Class", click: function() { alert('hi'); } });
From the docs:
As of jQuery 1.4, we can pass a map of properties to the second argument. This argument accepts a superset of properties that can be passed to the
.attr()
method. Furthermore, any event type can be passed in, and the following jQuery methods can be called: val, css, html, text, data, width, height, or offset. Note that Internet Explorer will not allow you to create an input element and change its type; you must specify the type using<input type="checkbox" />
for example.
精彩评论