简体   繁体   English

如果我删除并重新添加它,jQuery无法找到相同的div?

[英]jQuery unable to find same div if i remove and add it back?

I'm attempting to load different content into a div, the refresh code I have is quite similar to this: http://jsfiddle.net/TbJQ3/6/ but is more complex, it takes padding and magin etc into account. 我正在尝试将不同的内容加载到div中,刷新代码与此非常相似: http : //jsfiddle.net/TbJQ3/6/,但更为复杂,它考虑了填充和余量等。

The difference is that in my actual code, I used .load() to feed the content into my div rather than empty() and append(). 区别在于,在我的实际代码中,我使用.load()将内容提供给div而不是empty()和append()。

When my page first loaded, I call my resizeContainer() function to render my layout dynamically set the size of my DIVs, everything looks good. 第一次加载页面时,我调用resizeContainer()函数以动态设置布局以设置DIV的大小,一切看起来都很好。 I looked at firebug and this is what I got (all the sizes are there, set by my function): 我看着萤火虫,这就是我得到的(所有大小都在那,由我的功能设置):

<div id="divWrapper" class="fullscreen">
  <div id="divTop" class="Top " style="height: 0px;"> </div>
    <div id="divContainer" class="Container round" style="width: 1024px; height: 560px;">
      <div id="divContent" class="Content round" onclick="javascript: jQfn.testclick();" style="width: 701px; height: 542px;"> Content </div>
      <div id="divQuery" class="Query round" onclick="javascript: jQfn.testclick();" style="width: 292.2px; height: 558px;"> Query </div>
      <div id="divStatus" class="Status " onclick="javascript: jQfn.testclick();" style="width: 701px; float: left;"> Status </div>
    </div>
</div>

I made a test function to basically swap the content just to test the concept out... 我做了一个测试功能来基本上交换内容,只是为了测试概念。

jQfn.testclick = function(){
  if (jQ.busy == true) {
     console.log('Using busy.jsp');
     jQ('#divContainer').load('busy.jsp');
     jQ.busy = false;
  } else {
     console.log('Using login.jsp');
     jQ('#divContainer').load('login.jsp');
     jQ.busy = true;
  }
  jQ(window).trigger('resize');
}

However, if i click it twice just to swap it back to the original content, my resizeContainer function won't render anything at all. 但是,如果我单击两次只是将其交换回原始内容,我的resizeContainer函数将根本不会呈现任何内容。

I've been hammering this problem for the past 5+ hours now, I tried to use: 我过去5个小时以来一直在解决这个问题,我尝试使用:

  • empty, append 空,追加
  • detach 分离
  • calling the resize function in many different ways 以许多不同的方式调用resize函数
  • calling my custom resizeContainer function in many different ways 以许多不同的方式调用我的自定义resizeContainer函数
  • combination of all of the above and some more 以上所有和更多的组合

I noticed that after i loaded my content, jQuery was unable to find the divs that got reinserted anymore... which would explain why my resizeContainer function couldn't set the width and height properly... But why? 我注意到,在加载内容后,jQuery 无法找到重新插入的div ...这可以解释为什么我的resizeContainer函数无法正确设置宽度和高度...但是为什么? I could see it on the div on the screen as well as in the firebug... 我可以在屏幕以及萤火虫的div上看到它。

This is what I see after i swap the content, obviously the styles is not present, as my custom resizeContainer function couldn't find the div. 这是我交换内容后看到的,显然是样式不存在,因为我的自定义resizeContainer函数找不到div。

<div id="divWrapper" class="fullscreen">
  <div id="divTop" class="Top " style="height: 0px;"> </div>
  <div id="divContainer" class="Container round" style="width: 1024px; height: 560px;">
    <div id="divContent" class="Content round" onclick="javascript: jQfn.testclick();"> Content </div>
    <div id="divQuery" class="Query round" onclick="javascript: jQfn.testclick();"> Query </div>
    <div id="divStatus" class="Status " onclick="javascript: jQfn.testclick();" style="width: 701px; float: left;"> Status </div>
  </div>
</div>

The reason why I know jQuery couldn't find my divs, is because I used the following code, and it logged some empty element: 我知道jQuery找不到div的原因是因为我使用了以下代码,并且它记录了一些空元素:

var content = jQuery('#divContent');
console.log(content);

What could be wrong? 有什么事吗 I even tried to manually detach those DIVs before loading in the new content. 我什至尝试在加载新内容之前手动分离这些DIV。

Load is an Ajax method, and inserts content from external files, when using ajax methods and not the append methods you will in most cases have to use jQuery's live(); 加载是一种Ajax方法,它在使用ajax方法而不是append方法时从外部文件插入内容,在大多数情况下,您将不得不使用jQuery的live();。 to gain access to the inserted DOM elements. 获得对插入的DOM元素的访问权限。

Once the content is inserted the first time with Ajax, try using detach and one of the append methods instead of Ajax to reinsert it, otherwise it will not work, as load just reinserts new DOM elements, and not the content you just detached. 一旦使用Ajax首次插入了内容,请尝试使用detach和一个append方法(而不是Ajax)来重新插入它,否则它将无法正常工作,因为加载只是重新插入新的DOM元素,而不是刚刚分离的内容。

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

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