本文介绍了jquery .load()函数仅在第一次工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个正在使用jQuery和jQuery UI的个人网站作为工作网站

So I have a website I am working on just as a personal website that uses jQuery and jQuery UI

以前,我一直在使用隐藏的html代码,而只是使用jquery来显示它.但是它使我的html文件混乱,因此我想使用jquery的.load()来执行相同的操作,但是要从外部文件执行.

Previously I have been using hidden html code and just using jquery to show it.But its making my html file messy so I wanted to use jquery's .load() to do the same thing but from an external file.

现在,将其设置为.click函数.

Right now, its set to a .click function.

对于我隐藏的html来说,每当我单击一个特定元素时都会显示它.当您单击其他元素时,它会显示它.它隐藏了第一个.我通过有2个类的div来做到这一点.问题是,当我尝试将html加载到隐藏的div中,然后显示并隐藏它时,它仅在第一次使用时有效.

For my hidden html it shows it every time when I click a particular element.When you click on a different element it. It hides the first one. I am doing it by having a div with 2 classes. The problem is when I tried to load html into a hidden div, and then show it and hide it, it only worked the first time.

请多多指教,这是我的代码. #1有效,#2仅在首次点击时有效.并在之后每次将imagearea留为空白.

Enough talk, here is my code. #1 works , #2 only works on the first click. And leaves imagearea blank every time after.

$(".jquery").click(function(){

    clearImageArea();
    hideThumbnails(5);
    showThumbnails();
    $("#1").click(function(){

        $(".imagearea").html(js);
        $(".jscode").show(1000);
        $(".title").text("Extending jQuery");
    });
    $("#2").click(function(){
          $(".jquery2").empty();
          $(".jquery2").load("jqueryEx.html");


          var jquery2 =  $(".jquery2");
          $(".imagearea").html(jquery2);
          $(".jquery2").show(1000);
          $(".title").text("Extending Jquery Example");
      });


  });

现在我隐藏在HTML文件中的东西

now my hidden stuff in my html file

首先,我的html和js代码从jqueryEx.html加载到此处,并通过$(".hidden").hide();隐藏在我的javascript中的其他地方,然后通过.html()加载到imagearea中,并通过.show()

First my html and js code is loaded into here from jqueryEx.html and is being hidden elsewhere in my javascript via $(".hidden").hide(); and loaded then into into imagearea via .html() and shown via .show()

      <div class="jquery2 hidden">

      </div>

我的另一个div看起来像这样,通过单击#1进入图像区域

My other div looks like this which is put into imagearea by clicking on #1

    <div class="jscode hidden">
       <div class="block">

//很多js代码都逃到了html

//lots of js code escaped out into html

        </div> <!-- end of block-->
     </div>

在我的JS代码开头的其他地方,我有var js=$(".jscode");要将其加载到您之前看到的js变量中.

elsewhere in my JS code at the beginning I have var js=$(".jscode"); to load it into the js variable you saw earlier.

如果您想查看我正在从事的工作的过时示例请访问www.3realsoft.com(只有CS和JS才能使用技能)

if you want to see an out of date example of what I am working ongo to www.3realsoft.com (only cs and js work on skills)

如果您想查看我的代码的任何其他部分,只需询问.大部分都在我的网站上.

if you want to see any additional parts of my code, just ask. Most of it is there on my website though.

推荐答案

当我试图让按钮同时加载和刷新内容时,我在搜索结果中找到了该项目,并且负载正常工作,但刷新无效.

I got to this item in my search results, when I was trying to have a button both load and refresh the content, and the load was working but the refresh was not working.

这是该解决方案的简化版本,将Cache设置为false是关键.在另一个链接中找到了解决方案,但是我在这里发布了这个概念,因为如果Google将我放在这个项目中,寻找相同内容的其他人也可能会在这里找到自己. 向约翰·米利金(John Millikin)提出的建议,请务必先回答他并投票给他: -being-cached>停止缓存jQuery .load响应

Here's a shorter version of the solution, setting Cache to false was the key. Solution found over at this other link, but I'm posting this concept here because if Google dropped me in this item, others looking for the same will also probably find themselves here. Props to John Millikin, make sure to go over to his answer and upvote him: Stop jQuery .load response from being cached

<script type="text/javascript">
    $(document).ready(function () {

        $.ajaxSetup({
            // Disable caching of AJAX responses
            cache: false
        });

        $('.detail-expand').click(function () {

            var detailRowElement = $(this).closest('.session-row-tr').next();
            var detailElement = detailRowElement.find('.detail-row-div');
            var sessionId = detailElement.data("sessionId");

            detailElement.empty();
            detailElement.load('/Admin/WebLogPartial/' + sessionId, function () {
                $.bootstrapSortable(true, 'reversed');
            });

            detailRowElement.show();
        });

    });
</script>

这篇关于jquery .load()函数仅在第一次工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:30
查看更多