/
jquery.createHtml.js
86 lines (79 loc) · 3.35 KB
/
jquery.createHtml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*!
* jQuery create HTML plugin
* Original author: @efunneko
* Licensed under the MIT license
*/
// Simple jQuery plugin to make it a bit cleaner to add
// HTML using jQuery. Instead of doing:
//
// $("body").$("<div/>").append(
// $("<span/>", {id: 'mySpan'}).append(
// $("<b/>").html("My bold statement!")));
//
// You can do:
//
// $("body").$div().$span({id: 'mySpan'}).b("My bold statement!");
;(function ( $, window, document, undefined ) {
// Create the defaults once
var pluginName = 'createHtml',
defaults = {
},
elements = [
"a", "abbr", "address", "area", "article", "aside", "audio", "b", "base",
"bdi", "bdo", "blockquote", "body", "br", "button", "canvas", "caption",
"cite", "code", "col", "colgroup", "command", "data", "datalist", "dd",
"del", "details", "dfn", "div", "dl", "dt", "em", "embed", "fieldset",
"figcaption", "figure", "footer", "form", "h1", "h2", "h3", "h4", "h5",
"h6", "head", "header", "hgroup", "hr", "html", "i", "iframe", "img", "input",
"ins", "kbd", "keygen", "label", "legend", "li", "link", "main", "map", "mark", "math",
"menu", "meta", "meter", "nav", "noscript", "object", "ol", "optgroup", "option",
"output", "p", "param", "pre", "progress", "q", "rp", "rt", "ruby", "s",
"samp", "script", "section", "select", "small", "source", "span", "strong",
"style", "sub", "summary", "sup", "svg", "table", "tbody", "td", "textarea",
"tfoot", "th", "thead", "time", "title", "tr", "track", "u", "ul", "var",
"video", "wbr"
],
methods = {
configure: function(options) {
if (options.installParentFunctions) {
// Add $<element>_ functions that will return the parent object
// rather than itself
$.each(elements, function(i, elName) {
$.fn["$" + elName + "_"] = function(content, attrs) {
if (typeof(content) == 'object' && typeof(attrs) == 'undefined') {
attrs = content;
content = undefined;
}
var el = $("<" + elName + ">", attrs);
if (content && content != "") {
el.html(content);
}
var parent = $(this);
parent.append(el);
return parent;
}
});
}
}
};
$[pluginName] = function(method, options) {
if (methods[method]) {
methods[method](options);
}
};
// Add all element functions
$.each(elements, function(i, elName) {
$.fn["$" + elName] = function(content, attrs) {
if (typeof(content) == 'object' && typeof(attrs) == 'undefined') {
attrs = content;
content = undefined;
}
var el = $("<" + elName + ">", attrs);
if (typeof(content) !== "undefined" && content !== "") {
el.html(content);
}
$(this).append(el);
return el;
}
});
})( jQuery, window, document );