如何使用 jQuery .get() 获取原始 html 字符串

use*_*711 3 html ajax jquery get

我需要获取带有<div>标签和所有内容的原始 html,并将其放入变量字符串中。

所以如果我有一个单独的文件 test.html

<div>
<div><img src="images/htc_hero_wallpaper_01.jpg" width="20%" height="20%" alt="" /></div>
WORKS!!!
</div>
Run Code Online (Sandbox Code Playgroud)

我需要 jQuery 命令

$.ajax({
  url: "test.html",
  success: function(){

 var htmlraw = ?????
 alert(htmlraw)  /// should print the raw test.html
  }
});
Run Code Online (Sandbox Code Playgroud)

打印

<div>
<div><img src="images/htc_hero_wallpaper_01.jpg" width="20%" height="20%" alt="" /></div>
WORKS!!!
</div>
Run Code Online (Sandbox Code Playgroud)

Rei*_*ica 5

这可能太晚了,但正确的方法是将$.get ( dataType)的第四个参数设置为“text”:

$.get("test.html", null, function(html) {
    console.log(html);
}, "text");
Run Code Online (Sandbox Code Playgroud)

否则,它会自动检测响应是 HTML 并为您解析它。