- 項目 2
- 項目 3 ;
- アイテム 4
<script> && !( "outerHTML" in HTMLElement.prototype)) { <br>console.log("define innerHTML"); <br>HTMLElement.prototype.__defineSetter__("outerHTML",function(str){ <br>var フラグメント = document.createDocumentFragment (); <br>var div = document.createElement("div"); <br>div.innerHTML = str; <br>for(var i=0, n = div.childNodes.length; i< n; i ){ <BR>fragment.appendChild(div.childNodes[i]) <BR>this.parentNode.replaceChild(fragment, this); <BR> 🎜>HTMLElement .prototype.__defineGetter__("outerHTML",function(){ <BR>var tag = this.tagName; <BR>varattributes = this.attributes; <BR>var attr = []; <BR>// for(var name inattributs){//プロトタイプチェーン上のメンバーを走査します<BR>for(var i=0,n =attributs.length; i<n; i){//n< で指定された属性の数🎜>if(attributes[ i].specified){ <BR>attr.push(attributes[i].name '="'attributes[i].value '"'); <BR>} <BR>} <BR>return ((!! this.innerHTML) ? <BR>'<' タグ ' ' attr.join(' ') '>' this.innerHTML '</' タグ '>' : <br>' <' タグ ' attr.join(' ') '/>'); <BR>} <BR>var content = document.getElementById("content"); content.outerHTML) <BR></script>
sdfdsdfsd
の アウター HTMLコードを取得するとします。
コードをコピー コードは次のとおりです:
var _p = document.getElementById('outerID');
_P = _P.cloneNode(); var _DIV = document.createElement ();
_DIV.appendChild(_P);
alert(_DIV.innerHTML);
コードをコピー コードは次のとおりです:
/**
* Firefox 互換の innerHTML 次のコードを使用すると、Firefox で element.outerHTML を使用できるようになります
**/
if(window.HTMLElement) {
HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML){
var r=this.ownerDocument.createRange();
r .setStartBefore(this);
var df=r.createContextualFragment(sHTML);
return
}; prototype.__defineGetter__("outerHTML",function(){
var attr;
var attrs=this.attributes;
var str="<" this.tagName.toLowerCase();
for (var i=0;iattr=attrs[i];
if(attr.specified)
str =" " attr.name '="' 属性value '"';
}
if( !this.canHaveChildren)
return str ">";
return str ">" this.innerHTML "" this.tagName。 toLowerCase() ">";
});
HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){
switch(this.tagName.toLowerCase()){
case " area":
case "base":
case "basefont":
case "col":
case "frame":
case "hr":
case " img":
case "br":
case "input":
case "isindex":
case "link":
case "meta":
case "param" :
return false;
}
return true ;
}
insertAdjacentHTML の新しいソリューションについて互換性
* Firefox 互換の innerHTML 次のコードを使用すると、Firefox で element.outerHTML を使用できるようになります
**/
if(window.HTMLElement) {
HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML){
var r=this.ownerDocument.createRange();
r .setStartBefore(this);
var df=r.createContextualFragment(sHTML);
return
}; prototype.__defineGetter__("outerHTML",function(){
var attr;
var attrs=this.attributes;
var str="<" this.tagName.toLowerCase();
for (var i=0;i
if(attr.specified)
str =" " attr.name '="' 属性value '"';
}
if( !this.canHaveChildren)
return str ">";
return str ">" this.innerHTML "" this.tagName。 toLowerCase() ">";
});
HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){
switch(this.tagName.toLowerCase()){
case " area":
case "base":
case "basefont":
case "col":
case "frame":
case "hr":
case " img":
case "br":
case "input":
case "isindex":
case "link":
case "meta":
case "param" :
return false;
}
return true ;
}
insertAdjacentHTML の新しいソリューションについて互換性
コードをコピー
コードは次のとおりです: //---HTML コードを挿入コンポーネントの最後に function InsertHtm(op,code,isStart){ if (Dvbbs_IsIE5)
op.insertAdjacentHTML(isStart ? "afterbegin" : "afterEnd",code else); {
var range=op.ownerDocument.createRange();
range.setStartBefore(op );
if(isStart)
op. insertBefore(fragment,op.firstChild);
else
op.appendChild(fragment)
}
}
关于inner/outerHTML在NC6中的参考
DOM level 1 has no methods to allow for insertion of unparsed HTML into the document tree (as IE allows with insertAdjacentHTML or assignment to inner/outerHTML).NN6 (currently in beta as NN6PR3) know supports the .innerHTMLproperty of HTMLElements so that you can read or write the innerHTML of a page element like in IE4+.NN6 also provides a DOM level 2 compliant Range object to which a createContextualFragment('html source string')was added to spare DOM scripters the task of parsing html and creating DOM elements.You create a Range with var range = document.createRange();Then you should set its start point to the element where you want to insert the html for instance var someElement = document.getElementById('elementID'); range.setStartAfter(someElement);Then you create a document fragment from the html source to insert for example var docFrag = range.createContextualFragment('Note that the getter/setter feature is experimental and its syntax is subject to change.
HTMLElement.prototype.innerHTML setter = function (str) { var r = this.ownerDocument.createRange(); r.selectNodeContents(this); r.deleteContents(); var df = r.createContextualFragment(str); this.appendChild(df); return str;}HTMLElement.prototype.outerHTML setter = function (str) { var r = this.ownerDocument.createRange(); r.setStartBefore(this); var df = r.createContextualFragment(str); this.parentNode.replaceChild(df, this); return str;}
HTMLElement.prototype.innerHTML getter = function () { return getInnerHTML(this);}
function getInnerHTML(node) { var str = ""; for (var i=0; iHTMLElement.prototype.outerHTML getter = function () { return getOuterHTML(this)}
function getOuterHTML(node) { var str = ""; switch (node.nodeType) { case 1: // ELEMENT_NODE str = "<" node.nodeName; for (var i=0; iif (node.childNodes.length == 0 && leafElems[node.nodeName]) str = ">"; else { str = ">"; str = getInnerHTML(node); str = "<" node.nodeName ">" } break; case 3: //TEXT_NODE str = node.nodeValue; break; case 4: // CDATA_SECTION_NODE str = ""; break; case 5: // ENTITY_REFERENCE_NODE str = "&" node.nodeName ";" break;
case 8: // COMMENT_NODE str = "" break; }
return str;}
var _leafElems = ["IMG", "HR", "BR", "INPUT"];var leafElems = {};for (var i=0; i<_leafElems.length; i ) leafElems[_leafElems[i]] = true;
然后我们可以封成JS引用
if (/Mozilla/5.0/.test(navigator.userAgent)) document.write('
var range=op.ownerDocument.createRange();
range.setStartBefore(op );
if(isStart)
op. insertBefore(fragment,op.firstChild);
else
op.appendChild(fragment)
}
}
关于inner/outerHTML在NC6中的参考
DOM level 1 has no methods to allow for insertion of unparsed HTML into the document tree (as IE allows with insertAdjacentHTML or assignment to inner/outerHTML).NN6 (currently in beta as NN6PR3) know supports the .innerHTMLproperty of HTMLElements so that you can read or write the innerHTML of a page element like in IE4+.NN6 also provides a DOM level 2 compliant Range object to which a createContextualFragment('html source string')was added to spare DOM scripters the task of parsing html and creating DOM elements.You create a Range with var range = document.createRange();Then you should set its start point to the element where you want to insert the html for instance var someElement = document.getElementById('elementID'); range.setStartAfter(someElement);Then you create a document fragment from the html source to insert for example var docFrag = range.createContextualFragment('
Kibology for all.
');and insert it with DOM methods someElement.appendChild(docFrag);The Netscape JavaScript 1.5 version even provides so called setters for properties which together with the ability to prototype the DOM elements allows to emulate setting of outerHMTL for NN6: If you insert that script block you can then write cross browser code assigning to .innerHTML .outerHTMLfor instance document.body.innerHTML = 'Scriptology for all
';which works with both IE4/5 and NN6.The following provides getter functions for .outerHTMLto allow to read those properties in NN6 in a IE4/5 compatible way. Note that while the scheme of traversing the document tree should point you in the right direction the code example might not satisfy your needs as there are subtle difficulties when trying to reproduce the html source from the document tree. See for yourself whether you like the result and improve it as needed to cover other exceptions than those handled (for the empty elements and the textarea element).show document.documentElement.outerHTML|show document.body.outerHTML|show document.documentElement.innerHTML|show document.body.innerHTMLJavaScript.FAQTs.com
Kibology for all.
All for Kibology.
HTMLElement.prototype.innerHTML setter = function (str) { var r = this.ownerDocument.createRange(); r.selectNodeContents(this); r.deleteContents(); var df = r.createContextualFragment(str); this.appendChild(df); return str;}HTMLElement.prototype.outerHTML setter = function (str) { var r = this.ownerDocument.createRange(); r.setStartBefore(this); var df = r.createContextualFragment(str); this.parentNode.replaceChild(df, this); return str;}
HTMLElement.prototype.innerHTML getter = function () { return getInnerHTML(this);}
function getInnerHTML(node) { var str = ""; for (var i=0; i
function getOuterHTML(node) { var str = ""; switch (node.nodeType) { case 1: // ELEMENT_NODE str = "<" node.nodeName; for (var i=0; i
case 8: // COMMENT_NODE str = "" break; }
return str;}
var _leafElems = ["IMG", "HR", "BR", "INPUT"];var leafElems = {};for (var i=0; i<_leafElems.length; i ) leafElems[_leafElems[i]] = true;
然后我们可以封成JS引用
if (/Mozilla/5.0/.test(navigator.userAgent)) document.write('