javascript 使用 JQuery 刷新页面的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6627273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh part of a page using JQuery
提问by rolling stone
Is this a valid way to refresh part of webpage:
这是刷新部分网页的有效方法吗:
$("#content").load(location.href+" #content>*","");
Note that I'm not requesting any new data here, but basically re-loading the content of a div as part of an .ajax success function.
请注意,我不是在这里请求任何新数据,而是基本上重新加载 div 的内容作为 .ajax 成功函数的一部分。
This seems much easier than requesting data through an ajax function and loading that into the page, but I'm wondering if there are any drawbacks or issues with this method.
这似乎比通过 ajax 函数请求数据并将其加载到页面中容易得多,但我想知道这种方法是否有任何缺点或问题。
采纳答案by rolling stone
On this page: http://blog.mediasoft.be/partial-page-refresh-with-ajax-and-jquery/it appears that some people are having trouble getting this to work on IE, so I'm going to assume it's not 100% reliable yet and will continue to use the standard way of refreshing content using JQuery for now.
在此页面上:http: //blog.mediasoft.be/partial-page-refresh-with-ajax-and-jquery/似乎有些人在 IE 上无法使用它,所以我假设它现在还不是 100% 可靠的,现在将继续使用使用 JQuery 刷新内容的标准方式。
回答by Darin Dimitrov
This seems much easier than requesting data through an ajax function and loading that into the page,
这似乎比通过 ajax 函数请求数据并将其加载到页面中容易得多,
The .load()
method sends an AJAX request, so no, it's not easier, it's absolutely the same :-) In addition to the standard $.get()
, $.post()
and $.ajax()
methods it allows you to provide a selector so that you can fetch only some portion of the returned HTML during the AJAX request. Maybe it is this that makes it more convenient in some situations. But behind the scenes all of those method end up calling $.ajax()
. They are just shorthands.
该.load()
方法发送一个 AJAX 请求,所以不,这并不容易,它完全相同 :-) 除了标准$.get()
,$.post()
和$.ajax()
方法之外,它还允许您提供一个选择器,以便您可以在此期间仅获取返回的 HTML 的一部分AJAX 请求。也许正是这样,在某些情况下更方便。但在幕后,所有这些方法最终都会调用$.ajax()
. 它们只是速记。
回答by VajNyiaj
If you want to refresh part of a web page, say the content inside a element which has an ID or class of "content" : <div id="content" class="content">Content to refresh via ajax...</div>
如果要刷新网页的一部分,请说具有 ID 或“内容”类的元素内的内容: <div id="content" class="content">Content to refresh via ajax...</div>
$('#content').load('ajax/newcontent.html');
$('#content').load('ajax/newcontent.html');
or
或者
$('.content').load('ajax/newcontent.html');
$('.content').load('ajax/newcontent.html');
The content returned from the url "ajax/newcontent.html" will replace the content inside the div element with an id or class = "content".
从 url "ajax/newcontent.html" 返回的内容将用 id 或 class = "content" 替换 div 元素内的内容。
回答by VajNyiaj
Thanks @aruss. Just to be clear I'm not returning any HTML during the load action, but rather loading HTML from a div that's in the current webpage. – rolling stone 11 mins ago
谢谢@aruss。为了清楚起见,我在加载操作期间没有返回任何 HTML,而是从当前网页中的 div 加载 HTML。– 滚石 11 分钟前
--
——
Then there is no need to use the load() method. It's as simple as
那么就不需要使用 load() 方法了。就这么简单
$('#content').empty().html($('#idOfContentNeeded').html());
$('#content').empty().html($('#idOfContentNeeded').html());
回答by aruss
Actually there are no issues, the load
action is a wrapper for jQuery.ajax
it makes also some validations and assigns the html to the selected element.
实际上没有问题,该load
操作是一个包装器,jQuery.ajax
它也进行一些验证并将 html 分配给所选元素。
回答by Rob Raisch
You could reload a section of the visible page using iframes, if you can accept their limitations.
如果您可以接受它们的限制,您可以使用 iframe 重新加载可见页面的一部分。