Jquery $get / $ajax不能在所有浏览器中工作

时间:2022-06-24 19:40:28

I have code like below:

我有如下代码:

$('a.load-more').on("click",function(){
    $.get($('a.load-more').attr('href'), function(data) {
   $(".next-page").remove();
   $('.block-grid').append(data);
   event.preventDefault();
});

The html:

html:

<li class="next-page">
<a href="http://example.com/ajax_all/" class="load-more">Load More →</a>
</li>

Which as you can see, takes the url of the ajax content from the .load-more element, passes it to the $get method which then pulls in the content and appends it into the current page.

如您所见,它从.load-more元素获取ajax内容的url,并将其传递给$get方法,然后获取内容并将其附加到当前页面。

The odd thing is though, this is working in Chrome but not Firefox or Safari and there are no js errors in the inspectors for those browsers.

奇怪的是,这是在Chrome上工作,而不是在Firefox或Safari上,检查器中没有这些浏览器的js错误。

Instead of pulling the content in using ajax, it just goes to the url http://example.com/ajax_all/ and displays the content of that.

它没有使用ajax拉出内容,而是访问url http://example.com/ajax_all/并显示内容。

I'm stumped as to why it would work in Chrome and not Safari or firefox.

我被难住了为什么它会在Chrome而不是Safari或firefox中运行。

2 个解决方案

#1


5  

You script is invalid, or pasted wrong

您的脚本无效,或粘贴错误。

$('a.load-more').on("click", function () {
    $.get($('a.load-more').attr('href'), function (data) {
        $(".next-page").remove();
        $('.block-grid').append(data);
        event.preventDefault();

});

After fixing that it shows that your event.preventDefault(); is happening inside of the get() which is asynchronous.

修复后,它显示您的event.preventDefault();在异步的get()内部发生。

$('a.load-more').on("click", function () {
   $.get($('a.load-more').attr('href'), function (data) {
      $(".next-page").remove();
      $('.block-grid').append(data);
      event.preventDefault();
   });
});

Placing the preventDefault outside of the call back should fix your problem.

将preventDefault置于回调之外应该可以修复您的问题。

$('a.load-more').on("click", function (e) {
    $.get($('a.load-more').attr('href'), function (data) {
        $(".next-page").remove();
        $('.block-grid').append(data);

    });
    e.preventDefault();
});

#2


3  

Your problem is the word event, it must be in the parameters of click callback.

您的问题是单词事件,它必须在单击callback的参数中。

Try that:

试一试:

$('a.load-more').on("click", function(e){
    e.preventDefault();
    $.get($('a.load-more').attr('href'), function(data) {
        $(".next-page").remove();
        $('.block-grid').append(data);
    });
});

#1


5  

You script is invalid, or pasted wrong

您的脚本无效,或粘贴错误。

$('a.load-more').on("click", function () {
    $.get($('a.load-more').attr('href'), function (data) {
        $(".next-page").remove();
        $('.block-grid').append(data);
        event.preventDefault();

});

After fixing that it shows that your event.preventDefault(); is happening inside of the get() which is asynchronous.

修复后,它显示您的event.preventDefault();在异步的get()内部发生。

$('a.load-more').on("click", function () {
   $.get($('a.load-more').attr('href'), function (data) {
      $(".next-page").remove();
      $('.block-grid').append(data);
      event.preventDefault();
   });
});

Placing the preventDefault outside of the call back should fix your problem.

将preventDefault置于回调之外应该可以修复您的问题。

$('a.load-more').on("click", function (e) {
    $.get($('a.load-more').attr('href'), function (data) {
        $(".next-page").remove();
        $('.block-grid').append(data);

    });
    e.preventDefault();
});

#2


3  

Your problem is the word event, it must be in the parameters of click callback.

您的问题是单词事件,它必须在单击callback的参数中。

Try that:

试一试:

$('a.load-more').on("click", function(e){
    e.preventDefault();
    $.get($('a.load-more').attr('href'), function(data) {
        $(".next-page").remove();
        $('.block-grid').append(data);
    });
});