append: function() {
var clone = this.size() > 1;
var a = $.clean(arguments);
- return this.each(function(){
+ return this.domManip(function(){
for ( var i = 0; i < a.length; i++ ) {
this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
}
prepend: function() {
var clone = this.size() > 1;
var a = $.clean(arguments);
- return this.each(function(){
+ return this.domManip(function(){
for ( var i = a.length - 1; i >= 0; i-- ) {
this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
}
var r = [];
for ( var i = 0; i < a.length; i++ ) {
if ( a[i].constructor == String ) {
- if ( a[i].indexOf("<tr") == 0 ) {
+
+ if ( !a[i].indexOf("<tr") ) {
var tr = true;
a[i] = "<table>" + a[i] + "</table>";
+ } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
+ var td = true;
+ a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
}
+
var div = document.createElement("div");
div.innerHTML = a[i];
- if ( tr ) {
+
+ if ( tr || td ) {
div = div.firstChild.firstChild;
+ if ( td ) {
+ div = div.firstChild;
+ }
}
+
for ( var j = 0; j < div.childNodes.length; j++ ) {
r[r.length] = div.childNodes[j];
}
return r;
};
+$.fn = {};
+
+/**
+ * A wrapper function for each() to be used by append and prepend.
+ * Handles cases where you're trying to modify the inner contents of
+ * a table, when you actually need to work with the tbody.
+ */
+$.fn.domManip = function(fn){
+ return this.each(function(){
+ var obj = this;
+
+ if ( this.nodeName == 'TABLE' ) {
+ if ( !this.firstChild ) {
+ this.appendChild( document.createElement("tbody") );
+ }
+ obj = this.firstChild;
+ }
+
+ $.apply( obj, fn );
+ });
+};
+
$.g = {
'': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
'#': "a.getAttribute('id') && a.getAttribute('id').nodeValue == m[2]",
"[": "$.Select(m[2],a).length > 0"
};
-$.fn = {};
-
$.Select = function( t, context ) {
context = context || $.context || document;
if ( t.constructor != String ) {