简体   繁体   English

jQuery不喜欢.on和.append吗?

[英]jQuery doesn't like .on and .append?

I have a weird problem. 我有一个奇怪的问题。 I'm currently working on loading posts using PHP, AJAX and MySQL. 我目前正在使用PHP,AJAX和MySQL加载帖子。 The code structure itself looks like this: 代码结构本身如下所示:

My code structure 我的代码结构

--- main.js --- -main.js-

$(window).load(function(){
     initAjax();
});

--- ajax.js --- -ajax.js-

function initAjax(){

//   Toggles a navigation

     $(document).on('click', '.btn.open', function(){
         ... toggles a window ...
     });

//   Add new posts

     $(document).on('click', '.btn.refresh', function(){
         $.ajax({
              ... ajax stuff ...,
              success: function(html){
                   // Show new posts
                   $('.post_container').prepend(html);
              }
         });
     });
}

So what is the problem? 那是什么问题呢?

When I append new posts they'll show up, but I am not able to click .btn.open anymore - Shouldn't 'on()' fix this? 当我添加新帖子时,它们会出现,但我无法再单击.btn.open-'on()'是否可以解决此问题? When I go to the Google Chrome console. 当我转到Google Chrome控制台时。

Does somebody know a potential way to solve the problem? 有人知道解决问题的潜在方法吗?

Edit: 编辑:

  1. The appended posts are the same as the default loaded posts! 附加的帖子与默认加载的帖子相同!
  2. '.btn.open' exists (div with class="btn open") “ .btn.open”存在(具有class =“ btn open”的div)
  3. I am using jQuery v2.0.3 ( so .on should work! , .live and .delegate were replaced by .on! ) 我正在使用jQuery v2.0.3( 因此.on应该可以工作! ,. live和.delegate被.on!代替
  4. Removed an error message that was created by a corrupted Chrome extention = No change. 删除了由损坏的Chrome扩展程序创建的错误消息=不变。
  5. Created a .gif showing the problem in action: http://d.pr/i/cJB3 创建了一个.gif来显示问题所在: http : //d.pr/i/cJB3
  6. FIXED! 固定! @ cmorrissey found a small solution by replacing $(document) with $('body') @ cmorrissey通过用$('body')代替$(document)找到了一个小解决方案

    BUT

  7. This fix doesn't seem to be a perfect solution since $(document) normally has to work! 由于$(document)通常必须正常工作,因此此修补程序似乎不是完美的解决方案! Since I want clean code, I am totally going to try out @ Potherca 's Short, Self Contained, Correct, Example method and probably I'll find the solution this way. 由于我想要干净的代码,因此我将完全尝试@ Potherca简短,自包含,正确,示例方法,也许我会以这种方式找到解决方案。 Thanks 谢谢

You need to use 'body' or document.body instead of document . 您需要使用'body'document.body而不是document

    $('body').on('click', '.btn.refresh', function(){
         $.ajax({
              ... ajax stuff ...,
              success: function(html){
                   // Show new posts
                   $('.post_container').prepend(html);
              }
         });
     });

Reason: The addition of content to the body doesn't bubble up to the document level ( http://bugs.jquery.com/ticket/11621 ), it looks like you can also use window 原因:向正文添加的内容不会达到文档级别( http://bugs.jquery.com/ticket/11621 ),看起来您也可以使用window

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

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