jQuery的AJAX加载方法:只有一个请求多个元素?多个、只有一个、加载、元素

2023-09-11 01:35:11 作者:陌路△离殇

嘿, 我有我的第10页ul.rating元素。我想刷新这些元素的每一分钟。

  VAR重载= 60000;
VAR当前页= window.location的;
的setInterval(函数(){

    $('ul.ratings')负载(当前页+.ul.ratings> *,函数(){
        //回电话
    });

},重装);
 

现在我有以下两个问题。

我需要找到除了爱情之外重新加载每个元素新的。现在,我可能重装相同ul.ratings元素在我的网页上的所有ul.ratings元素。因此,必须有一些方法来使用索引()或一些其他的jquery方法来重新加载所述第一ul.ratings元件与第一ul.ratings元件和重装与第五ul.ratings elment第五ul.ratings元件。

整件事可能是一个相当糟糕的方式做到这一点,但我想在我的情况下,有没有更好的办法。是否有可能做负载的方法只有一次,并抓住每一个ul.ratings元,更换正确的?现在我做的负载要求,如果有我的第10页ul.ratings元素。

感谢您的帮助!

解决方案

你有过,你调用的URL控制?你可以把它仅返回您想要的数据?在这种情况下,让其返回JSON或XML并重装UL与该信息的。

否则,您可以使用$不用彷徨功能,加载jQuery中的HTML,找到UL的自己:

  VAR ulorigs = $('ul.ratings');
$获得(URL,功能(数据){
  变种含量= $(数据);
  $('ul.ratings,内容)。每个(函数(IND,ELEM){
    $(ulorigs.get(0))的HTML($(ELEM)的.html())。
  });
});
 
3 jQuery AJAX

我没有运行此code自己。的

Hey, I have 10 "ul.rating" elements on my page. I want to refresh those elements every minute.

var reload = 60000;
var currentpage = window.location;
setInterval(function() {

    $('ul.ratings').load(currentpage + " .ul.ratings >*", function() {
        //callback
    });

}, reload);

Now I have the following two problems.

I need to find away to reload each element new. Right now I'm probably reloading the SAME ul.ratings element for all ul.ratings elements on my page. So there must be some way to use index() or some other jquery method to reload the first ul.ratings element with the first ul.ratings element and reload the fifth ul.ratings element with the fifth ul.ratings elment.

The whole thing is probably a rather bad way to do this, but I guess in my case there is no better way. Is it possible to do the load-method just ONCE and grab each ul.ratings element and replace the correct one? Right now I'm doing the load-calls if there are 10 ul.ratings elements on my page.

Thank you for your help!

解决方案

Do you have control over the URL that you call? Can you make it return only the data that you want? In that case, let it return JSON or XML and reload your ul's with that information.

Otherwise, you could use the $.get function, load the HTML in jquery and find the ul's yourself:

var ulorigs = $('ul.ratings');
$.get('url', function (data) { 
  var content = $(data);
  $('ul.ratings', content).each(function(ind,elem) {
    $(ulorigs.get(0)).html($(elem).html());
  });
});

I did not run this code myself.