आप इसे के प्रोटोटाइप को बदलने के द्वारा देशी HTMLTableRowElement को यह कार्यक्षमता जोड़ सकते हैं:
HTMLTableRowElement.prototype.insertCell = (function(oldInsertCell) {
return function(index) {
if (this.parentElement.tagName.toUpperCase() == "THEAD") {
if (index < -1 || index > this.cells.length) {
// This case is suppose to throw a DOMException, but we can't construct one
// Just let the real function do it.
} else {
let th = document.createElement("TH");
if (arguments.length == 0 || index == -1 || index == this.cells.length) {
return this.appendChild(th);
} else {
return this.insertBefore(th, this.children[index]);
}
}
}
return oldInsertCell.apply(this, arguments);
}
})(HTMLTableRowElement.prototype.insertCell);
के बाद इस कोड चलता है, किसी भी नए HTMLTableRowElements ("td" टैग) यदि माता-पिता एक thead टैग है देखने के लिए जाँच करेगा। यदि ऐसा है, तो यह insertCell के समान कार्यक्षमता करेगा, लेकिन इसके बजाय एक वें टैग का उपयोग करेगा। यदि नहीं, तो यह केवल मूल insertCell कार्यक्षमता का उपयोग करेगा।
ध्यान दें कि it is generally not recommended to extend the native objects.
स्रोत
2017-11-24 23:21:05
की सलाह दी हो, 'th' के रूप में तत्काल बच्चे को' thead' अमान्य है और अलग-अलग ब्राउज़र में अवांछित परिणाम हो सकता है। वैध लेआउट इस तरह है: 'table-> thead-> tr-> th' –
क्षमा करें, मेरा बुरा, मेरा मतलब था थैड के अंदर' th'। –
मैंने 'thead' के अंदर 'tr' के साथ अपना उत्तर तय किया :-) –