繁体   English   中英

如何使用jQuery读取外部html文件并将其存储到字符串变量中? 显示错误

[英]How to read external html file and store it to string variable using jQuery? Showing Error

我正在尝试动态生成 W​​eb 应用程序的代码
我想读取外部 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'/>

查询

$(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
});
this verion is working fine for me you have to close `);` 
$(document).ready(function()
    {
      $.get("leads/leads_custom_box.php", function(result)
       {
          alert(result);
       });
    });

使用jQuery

$.get("http://localhost/something/something.html", function (result) {
     html = result;
     text = $(result).text();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM