如何使用 jQuery .get() 获取原始 html 字符串
我需要获取带有
标签和所有内容的原始 html,并将其放入变量字符串中。因此,如果我有一个单独的文件 test.html,
<div>
<div><img src="images/htc_hero_wallpaper_01.jpg" width="20%" height="20%" alt="" /></div>
WORKS!!!
</div>
我需要 jQuery 命令
$.ajax({
url: "test.html",
success: function(){
var htmlraw = ?????
alert(htmlraw) /// should print the raw test.html
}
});
来打印
<div>
<div><img src="images/htc_hero_wallpaper_01.jpg" width="20%" height="20%" alt="" /></div>
WORKS!!!
</div>
I need to get raw html with <div>
tags and everything, and put it in a variable string.
So if I have a separate file test.html
<div>
<div><img src="images/htc_hero_wallpaper_01.jpg" width="20%" height="20%" alt="" /></div>
WORKS!!!
</div>
I need the jQuery command
$.ajax({
url: "test.html",
success: function(){
var htmlraw = ?????
alert(htmlraw) /// should print the raw test.html
}
});
to print
<div>
<div><img src="images/htc_hero_wallpaper_01.jpg" width="20%" height="20%" alt="" /></div>
WORKS!!!
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能太晚了,但正确的方法是将 第四个参数设置为 $.get (
dataType
) 为“text”:否则,它将自动检测响应是否为 HTML 并为您解析它。
This is probably way too late, but the correct way to do this is by setting the fourth parameter to $.get (the
dataType
) to "text":Otherwise, it will auto-detect that the response is HTML and parse it for you.
你应该看看 jQuery http://api.jquery.com/html/
You should take a look at .html() from jQuery http://api.jquery.com/html/
如果您想获取文件中特定元素的 html,可以找到该元素并将其附加到新元素。这样,您将能够使用
.html()
获取原始 html 字符串If you want to get the html of a specific element in your file, you can find the element and append it to a new element. This way, you will be able to get the raw html string with
.html()