replace Html
1681 ワード
JAVA SCRIPT:
//This is is much faster than using(el.innerHTML=str)when there are many
existing descendants,because in some browses,innese HTML spends mch longer
removing existing elements than it creating new ones.*/
function replace Html
(el,)
)
{
var oldEl=
(
typeof el==
「string」?document.
getElemenntById
(el
):はい
)0
var newEl=document.
createElement
(oldEl.
nodeName
)0
//Preserve the element's id and class(other properties are lost)
newEl.
id=oldEl.
id;
newEl.
className=oldEl.
クラスName;
//Replace the old with the new
newEl.
innerHTML=html
oldEl.
parent Node.
replacceChild
(newEl,oldEl
)0
//*Since we just removed the old element from the DOM,return a reference
to the new element,which can be used to restore variable references.*/
return newEl
}
another way:
//This is is much faster than using(el.innerHTML=str)when there are many
existing descendants,because in some browses,innese HTML spends mch longer
removing existing elements than it creating new ones.*/
function replace Html
(el,)
)
{
var oldEl=
(
typeof el==
「string」?document.
getElemenntById
(el
):はい
)0
var newEl=document.
createElement
(oldEl.
nodeName
)0
//Preserve the element's id and class(other properties are lost)
newEl.
id=oldEl.
id;
newEl.
className=oldEl.
クラスName;
//Replace the old with the new
newEl.
innerHTML=html
oldEl.
parent Node.
replacceChild
(newEl,oldEl
)0
//*Since we just removed the old element from the DOM,return a reference
to the new element,which can be used to restore variable references.*/
return newEl
}
another way:
function replaceHtml(el, html) {
var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
/*@cc_on // Pure innerHTML is slightly faster in IE
oldEl.innerHTML = html;
return oldEl;
@*/
var newEl = oldEl.cloneNode(false);
newEl.innerHTML = html;
oldEl.parentNode.replaceChild(newEl, oldEl);
/* Since we just removed the old element from the DOM, return a reference
to the new element, which can be used to restore variable references. */
return newEl;
};