我正在尝试为 Web 应用程序动态生成代码
我想读取外部 HTML 文件并将其存储到 Javascript 或 Jquery 中的字符串变量?
有什么有效的方法吗..?
HTML 文件 - Object-text.html
<div class="text-object">
<div class='text-area'>
<span class='content'>
<span class='title'> title</span><br/>
<span class='address'> address </span>
</span>
</div>
<div class='patch'></div>
</div>
<br class='float-clear'/>
JQUERY
$(document).ready(function()
{
$.get("templates/object-text.html", function(html_string)
{
alert(html_string); // this is not Working
});
});
在控制台中显示错误
文档元素之后的垃圾
等待回复。
提前致谢
最佳答案
更改了 JQUERY 代码
我已将额外参数添加到函数调用 $.get() 以指定 html 内容
$(document).ready(function()
{
$.get("templates/object-text.html", function(html_string)
{
alert(html_string);
},'html'); // this is the change now its working
});
关于javascript - 如何使用 jQuery 读取外部 html 文件并将其存储到字符串变量?显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39051658/