简体   繁体   English

如何更新单个div的html内容?

[英]How to update the html content of a single div?

I'm sure that this is a very simple problem, but I have multiple pages each with their own 'content' with page navigation at the bottom.我确信这是一个非常简单的问题,但我有多个页面,每个页面都有自己的“内容”,底部有页面导航。 Before I start coding a script to generate several different html files who all have head, body, and navigation footer code... how could I have only one instance of the navigation footer and have the links only update the content inside the 'content' div?在我开始编写脚本以生成几个不同的 html 文件之前,这些文件都有头部、主体和导航页脚代码......我怎么可能只有一个导航页脚实例并且链接只更新“内容”中的内容分区?

Very basic example of updating an element's content via JavaScript: 通过JavaScript更新元素内容的非常基本的示例:

<div id="content"></div>

<script>
    document.getElementById('content').innerHTML='<b>oh hai</b>';
</script>​​​​​​​​​​​​​

To do it when someone clicks on a link, you'd attach a function to the onclick handler for that link that does the updating and then returns false so the link won't do it's usual navigation. 要在有人单击链接时执行此操作,您可以将一个函数附加到该链接的onclick处理程序上,该函数会进行更新,然后返回false,因此该链接不会执行通常的导航。

If you don't want to have all the content loaded into a single file, you can use AJAX to retrieve content dynamically. 如果您不想将所有内容加载到单个文件中,则可以使用AJAX动态检索内容。 You may wish to use a library/framework like jQuery to simplify the coding of AJAX interactions. 您可能希望使用类似jQuery的库/框架来简化AJAX交互的编码。

You can do this with AJAX. 您可以使用AJAX进行此操作。 An example with jQuery is using the load function: jQuery的一个示例使用load函数:

http://api.jquery.com/load/ http://api.jquery.com/load/

This will fetch a given URL and load its contents into an element matched by a selector. 这将获取给定的URL,并将其内容加载到与选择器匹配的元素中。

This can be solved in a few different ways. 这可以通过几种不同的方式解决。 You'll either need to load all possible contents at once (easy to access content after load, but slow initial load), or you can asynchronously request content as your user requires it. 您将需要一次加载所有可能的内容(加载后易于访问内容,但初始加载速度很慢),或者您可以根据用户的要求异步请求内容。

1) Hardcode all content into one page 1)将所有内容硬编码到一页

By doing this, you'd have a selection of content blocks hidden on your page: 这样,您可以在页面上隐藏一些内容块:

<div class="content-blocks">
  <div class="content" id="content1">...</div>
  <div class="content" id="content2">...</div>
  ...
</div>

Then, each link would have an event handler to load the appropriate content into your main content element. 然后,每个链接都将具有一个事件处理程序,以将适当的内容加载到您的主要content元素中。

document.getElementById('content1-link').onclick = function() {
    document.getElementById('content-box').innerHTML = document.getElementById('content1').innerHTML
}

2) Make AJAX requests for content 2)提出AJAX内容请求

To do this, your various content blocks would be stored in external files, eg 'content1.html', 'content2.html', etc. I would highly recommend using a javascript library with AJAX support for this method, as they will handle differences in how browsers handle asynchronous requests. 为此,您的各种内容块将存储在外部文件中,例如“ content1.html”,“ content2.html”等。我强烈建议使用支持AJAX的javascript库对此方法进行处理,因为它们会处理差异浏览器如何处理异步请求。 Some, like jQuery, also provide convenience functions to do such tasks: 一些工具(例如jQuery)还提供了方便的功能来执行以下任务:

$('#content1-link').on('click',function(){
  $('#content-box').load('/path/to/content1.html');
});

3) Use include statements 3)使用include语句

This method has the ease of implementation of the first solution (doesn't rely on async requests), but it keeps your content in separate files, like the second solution. 此方法可以轻松实现第一个解决方案(不依赖异步请求),但是可以像第二个解决方案一样将您的内容保存在单独的文件中。 Basically, you utilize whatever type of include your server/language supports (eg SSI includes, PHP require , etc). 基本上,您可以利用服务器/语言支持的任何类型的包含(例如SSI包含,PHP require等)。 You would then create the event handlers as in the first option. 然后,您将像第一个选项一样创建事件处理程序。

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

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