.html()和.append()没有jQuery [英] .html() and .append() without jQuery

查看:109
本文介绍了.html()和.append()没有jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何在不使用jQuery的情况下使用这两个函数?



我使用预先编码的应用程序,我无法使用jQuery,而且我需要从一个div获取HTML,并使用JS将其移动到另一个div。

解决方案

要将HTML从一个div复制到另一个div,使用DOM。

  function copyHtml(source,destination){
var clone = source.ownerDocument === destination .ownerDocument
? source.cloneNode(true)
:destination.ownerDocument.importNode(source,true);
while(clone.firstChild){
destination.appendChild(clone.firstChild);


对于大多数应用程序, inSameDocument 总会是真的,所以你可能会忽略所有在错误时起作用的部分。如果您的应用在同一个域中通过JavaScript进行交互的多个框架,您可能需要保留它。



如果您想要替换HTML,您可以通过清空然后复制到它:
$ b $ pre $ function replaceHtml(source,destination){
while(destination.firstChild) {
destination.removeChild(destination.firstChild);
}
copyHtml(source,destination);
}


Can anyone tell me how can I use these two functions without using jQuery?

I am using a pre coded application that I cannot use jQuery in, and I need to take HTML from one div, and move it to another using JS.

解决方案

To copy HTML from one div to another, just use the DOM.

function copyHtml(source, destination) {
  var clone = source.ownerDocument === destination.ownerDocument
      ? source.cloneNode(true)
      : destination.ownerDocument.importNode(source, true);
  while (clone.firstChild) {
    destination.appendChild(clone.firstChild);
  }
}

For most apps, inSameDocument is always going to be true, so you can probably elide all the parts that function when it is false. If your app has multiple frames in the same domain interacting via JavaScript, you might want to keep it in.

If you want to replace HTML, you can do it by emptying the target and then copying into it:

function replaceHtml(source, destination) {
  while (destination.firstChild) {
    destination.removeChild(destination.firstChild);
  }
  copyHtml(source, destination);
}

这篇关于.html()和.append()没有jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆