如何使用jQuery从文件添加内容? [英] How to add content from file using jQuery?

查看:110
本文介绍了如何使用jQuery从文件添加内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < html>< head>< / head> 
< body>
< div id =content>
< / div>
< / body>
< / html>

另外我有一个内容文件(不是html文档,只是html代码):

 < div class =CodeRay> 
< div class =code>< pre>< span class =no> 1·/跨度>要求
....
< / pre>< / div>

我想将内容文件添加到html文档中,如下所示:

 < html>< head>< / head> 
< body>
< div id =content>
< div class =CodeRay>
< div class =code>< pre>< span class =no> 1·/跨度>要求
....
< / pre>< / div>
< / div>
< / body>
< / html>

如何使用jQuery完成此任务?此外,我需要运行本地localy。感谢。



编辑:本地意味着像这样file:/// E:/Work/ReadTheCode/main.html

解决方案

使用jQuery的 .load()函数。代码可能看起来像这样:

  $(document).ready(function(){
$( '#content')。load('contentFile.html');
});

如果您需要更多信息,这里是链接到jquery的加载文档。


I have main html document:

<html><head></head>
<body>
    <div id="content">
    </div>
</body>
</html>

Also I have a content file (not the html document, just html code):

<div class="CodeRay">
<div class="code"><pre><span class="no">   1</span> require
....
</pre></div>

I want to add the content file to html document like here:

<html><head></head>
    <body>
        <div id="content">
            <div class="CodeRay">
            <div class="code"><pre><span class="no">   1</span> require
            ....
            </pre></div>
        </div>
    </body>
</html>

How to use jQuery to accomplish this task? Also I need to run this site localy. Thanks.

EDIT: Locally means like this "file:///E:/Work/ReadTheCode/main.html"

解决方案

Use jQuery's .load() function. The code would probably look something like this:

$(document).ready(function(){
  $('#content').load('contentFile.html');
});

If you need more info, here is the link to jquery's load documentation.

这篇关于如何使用jQuery从文件添加内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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