jQuery.Ajax:$ .get和document.write替换现有标记 [英] jQuery.Ajax: $.get and document.write replace existing markup

查看:138
本文介绍了jQuery.Ajax:$ .get和document.write替换现有标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了一种使用jQuery Ajax将外部文件的内容包含到网站中的方法.

I recently found out a method to include the content of an external file into the website using jQuery Ajax.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
  $.get("http://something.com/content.txt", function(a) {
    document.write("<p>Some text! - " + a + "</p>")
  });
</script>

则输出如下:

<p>Some text! - Content from external file</p>

它工作正常,只不过这段代码将覆盖整个标记.其他所有内容都将被排除.

It works fine except that this code is overwriting the entire markup. Everything else will be excluded.

有什么办法解决这个问题吗?

Any ideas how to fix this?

推荐答案

document.write将替换所有内容.

您正在使用jQuery,因此,如果要保留现有代码,请尝试.append(),例如:

You're using jQuery so try .append() if you want to keep existing code, like this:

$.get("http://something.com/content.txt", function(a) {
  $("#element-where-inserting").append(a);
});

这篇关于jQuery.Ajax:$ .get和document.write替换现有标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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