`
jinweijie
  • 浏览: 7503 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

replaceHtml

阅读更多
JAVASCRIPT:
  1.  
  2. /* This is much faster than using (el.innerHTML = str) when there are many
  3. existing descendants, because in some browsers, innerHTML spends much longer
  4. removing existing elements than it does creating new ones. */
  5. function replaceHtml(el, html) {
  6.         var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
  7.         var newEl = document.createElement(oldEl.nodeName);
  8.         // Preserve the element's id and class (other properties are lost)
  9.         newEl.id = oldEl.id;
  10.         newEl.className = oldEl.className;
  11.         // Replace the old with the new
  12.         newEl.innerHTML = html;
  13.         oldEl.parentNode.replaceChild(newEl, oldEl);
  14.         /* Since we just removed the old element from the DOM, return a reference
  15.         to the new element, which can be used to restore variable references. */
  16.         return newEl;
  17. };
  18.  

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;
};
分享到:
评论

相关推荐

    ASP常用函数:ReplaceHTML

    ‘去掉HTML标记 Public Function ReplaceHTML(Textstr) Dim Str, re Str = Textstr Set re = New RegExp re.IgnoreCase = True re.Global = True re.Pattern = “<(.[^>]*)>” Str = re....

    js代码-正则,uuid生成,replaceHtml

    在提供的`main.js`文件中,可能包含了实现这些功能的代码,如正则表达式的实例、UUID生成的逻辑以及自定义的`replaceHtml`函数。`README.txt`文件可能对这些代码的功能和使用方法做了简要说明。通过阅读和理解这些...

    SQL中使用正则替换的自定义函数

    在提供的压缩包中,"ReplaceHtml_RegExp.sql"可能就是这样一个函数的定义。这个函数可能使用T-SQL编写,并结合了第三方库(如`Microsoft SQL Server CLR Functions`)来实现正则表达式支持。 安装自定义函数通常...

    gulp-html-replace:替换HTML中的构建块。 像useref一样,但是做对了

    gulp-html-replace 替换HTML中的构建块。 像useref一样,但是做对了。目录用法安装: npm install --save-dev gulp-html-replace 在您HTML文件中放置一些块: <!-- build:<name> -->Everything here will be ...

    parcel-plugin-change-file:parcel 的文件处理插件 \ Jump parcel bundler in html & Replace html string & Copy files at parcel builded

    在宗地构建结束时,更改index.html上下文,然后在outDir中复制静态文件 :building_construction: 安装 $ yarn add -D parcel-plugin-change-file :bookmark: Feature1:释经index.html 添加<!--[ your-code ]...

    PHP正则表达式清除html标签 str_replace

    preg_replace 可以清除html标签 可以配合str_replace 一起使用

    asp中实现清除html的函数

    这个函数是必需要的,很多... content=replacehtml(&#[^>]*;,,content) content=replacehtml(</?marquee[^>]*>,,content) content=replacehtml(</?object[^>]*>,,content) content=replacehtml(</?param

    replace.html

    replace

    Advanced Find and Replace

    Advanced Find and Replace是一款强大的文档处理工具,专为高效地搜索、替换和修改文本内容而设计。这款工具超越了标准文本编辑器的查找与替换功能,提供了一系列高级特性,使得在大量文档中进行复杂操作变得更加...

    asp 正则实现清除html文本格式的函数代码

    在`ClearHtml`函数中,可以看到一系列对`Zxj_ReplaceHtml`的调用,每次调用都是为了去除特定的HTML标签,例如`<marquee>`、`<object>`、`<param>`、`<embed>`等,这些都是常见的可以执行JavaScript代码或者显示动态...

    文本替换工具replace

    当你需要在一个目录下的一系列文本文件(如.txt、.java、.html等)中统一替换某一特定文本字符串时,"replace"能轻松胜任。只需指定源目录、目标文本以及要替换成的新文本,程序就会遍历目录下的所有文件,执行替换...

    通过 Dom 方法提高 innerHTML 性能

    为了提高性能,我们可以采用一种名为 `replaceHtml()` 的 DOM 方法。 `replaceHtml()` 函数的主要目的是通过创建一个新的元素,将新内容设置为该元素的 `innerHTML`,然后用这个新元素替换原有的元素,来优化 `...

    location.replace

    通常,这些HTML文件会包含JavaScript代码,当用户执行某些操作(如点击按钮)时,会调用`location.replace()`方法,从而改变当前页面的URL。 例如,`index.html`可能有这样的代码: ```html <!DOCTYPE html> <html>...

    Actual Search & Replace

    3. **多种文件类型处理**:软件不仅限于纯文本文件,还能处理包括HTML、XML、CSV、JavaScript等多种格式的文件,这对于网站开发者和数据分析师来说是个福音。 4. **预览与确认**:在执行替换操作之前,用户可以预览...

Global site tag (gtag.js) - Google Analytics