简体   繁体   English

为什么我的 jquery.load() 函数不工作?

[英]Why is my jquery .load() function not working?

I have a button on my site which, when clicked on, should load html inside a div container.我的网站上有一个按钮,单击该按钮时,应将 html 加载到 div 容器中。

I'm using the following code for the button:我正在为按钮使用以下代码:

<div id="nextnr1"><a href = "#">Next&gt;&gt;</a></div>

The following code to execute the loading when someone clicks on the div:当有人点击 div 时执行加载的代码如下:

<script type="text/javascript">
    $(document).ready(function() {
        $("#nextnr1").mousedown(function() {
            $("#Container").load("wrappers.php?currenttab=1&shift=1");
        });
    });​
</script>

This is the code for the container where the HTML is loaded into:这是加载 HTML 的容器的代码:

<div id="Container"></div>

The problem is that all of the above code is working most of the time.问题是以上所有代码大部分时间都在工作。 Only once in a while the function .load is not doing his job.只是偶尔函数.load没有完成他的工作。 This is for similar circumstance because it is about the same page.这是针对类似情况,因为它是关于同一页面的。

I checked that when there is a click the function was called so: $(document).ready and $("#nextnr1").mousedown(function() {..} are doing their job. Only .load is NOT doing its job because I checked that there was no HTTP request sent to the server.我检查了当有点击时函数是这样调用的: $(document).ready$("#nextnr1").mousedown(function() {..}正在做他们的工作。只有.load没有做它的工作作业,因为我检查过没有 HTTP 请求发送到服务器。

If anyone can help me out with this problem I would be very very thankful.如果有人可以帮助我解决这个问题,我将非常感激。

UPDATE 1更新 1

I tried the message box (see code below) and it shows that when the load was not executed it also didn't show the message box Load was performed.我尝试了消息框(见下面的代码),它显示当加载未执行时它也没有显示消息框加载已执行。 I also saw that after 2/3 minutes suddenly the message box showed up and it reloaded.我还看到 2/3 分钟后突然出现消息框并重新加载。 However it does work but sometimes there is a huge delay.但是它确实有效,但有时会有很大的延迟。

$(document).ready(function() {
   $("#renextnr2").click(function() {
      $("#Container").load("wrappers.php?currenttab=2&shift=1", function() {
           alert("Load was performed.");
      });
   });
});

first off, try using 'click' instead of mousedown.首先,尝试使用“点击”而不是 mousedown。

second, try hooking the 'click' event to the link instead of the div:其次,尝试将“点击”事件挂钩到链接而不是 div:

<div><a id="nextnr1" href = "#">Next&gt;&gt;</a></div>

you may need to stop the event from propagating/default processing:您可能需要停止传播/默认处理事件:

<div><a id="nextnr1" href="#">Next&gt;&gt;</a></div>

<script type="text/javascript">

$(document).ready(function() {
$("#nextnr1").click(function(e) {
  e.preventDefault();
  e.stopPropagation();
  $("#Container").load("wrappers.php?currenttab=1&shift=1");
});});

</script>

First thing to start off with is to add an alert to debug whether the event is even being fired, as shown below.首先要做的是添加一个警报来调试事件是否被触发,如下所示。 This will give better insight on what area to look closer at to determine the problem.这将使您更好地了解需要仔细观察的区域以确定问题所在。

$('#Container').load('wrappers.php?currenttab=1&shift=1', function() {
  alert('Load was performed.');
});

I added a randon number at the end of the URL which solved the problem.我在解决问题的 URL 末尾添加了一个随机数。

The jquery load() function can behave inconsistently if there is a problem in your server-side code (PHP).如果您的服务器端代码 (PHP) 存在问题,则 jquery load() 函数的行为可能会不一致。

I had an issue where load() was working in my local site, but was not working consistently on my host.我遇到了一个问题,load() 在我的本地站点上运行,但在我的主机上运行不一致。 It turned out I had written some bad PHP that didn't cause any problem in my local environment, but caused problems when I deployed to the host.原来我写了一些糟糕的 PHP,在我的本地环境中没有引起任何问题,但是当我部署到主机上时却引起了问题。

Check to make sure the URL you are loading with javascript can load in your browser if you visit the URL directly.如果您直接访问该 URL,请检查以确保您使用 javascript 加载的 URL 可以加载到您的浏览器中。 That will tell you if it's a javascript or server-side issue.这会告诉您这是 JavaScript 问题还是服务器端问题。

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

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