jQuery的阿贾克斯:插入到< DIV>有时会失败?有时会、阿贾克斯、jQuery、lt

2023-09-10 20:55:27 作者:再笑虎牙就掉啦

我使用jQuery的阿贾克斯大量更新HTML页面的股利。阿贾克斯是包含在$(文件).read(...),如以下内容:

  $(文件)。就绪(函数(){
  $('a.button)。点击(函数(){
    VAR URL = $(本).attr(HREF');
    $('#内容)的负载(URL)。
    返回false;
  });
});
 

不过,点击按钮后,阿贾克斯有时会失败,显示的结果,在整个页面,而不是在内容股利。这经常发生在页面加载或刷新。但我想,$(文件)。就绪(...)将prevent这种情况的发生。出了什么问题?

解决方案   

阿贾克斯有时会失败,显示的结果,在整个页

这表明,超链接没有与选择 a.button 选择。请确保您要通过AJAX加载的超链接具有类名按钮,像这样:

 <一类=按钮的href =...>
 
jquery怎么设置div高度

如果你想所有超链接通过AJAX加载,删除的按钮选择。

  $(文件)。就绪(函数(){
  $('A')。点击(函数(){
    ......
  });
});
 

如果某些超链接是动态页面加载后,由另一个脚本创建,试试这个:

  $(文件)。就绪(函数(){
  $('身体')。在('点击','A',函数(){
    ......
  });
});
 

I uses jQuery Ajax a lot to update a div in HTML pages. The ajax is included in $(document).read(...) such as the following:

$(document).ready(function() {
  $('a.button').click(function() {
    var url = $(this).attr('href');
    $('#content').load(url);
    return false;
  });
});

However, after clicking the button, Ajax sometimes fails and it displays the result in the whole page, instead in the 'content' div. This happened frequently when the page is loading or refreshed. But I suppose that $(document).ready(...) would prevent this from happening. What went wrong?

解决方案

Ajax sometimes fails and it displays the result in the whole page

This suggest that the hyperlink was not selected with the selector a.button. Make sure that the hyperlinks you want loaded via AJAX has the class name button, like so:

<a class="button" href="...">

If you want ALL hyperlinks to load via AJAX, remove the button selector.

$(document).ready(function() {
  $('a').click(function() {
    ... ...
  });
});

If some hyperlinks are dynamically created after page load by another script, try this:

$(document).ready(function() {
  $('body').on('click', 'a', function() {
    ... ...
  });
});