代码之家  ›  专栏  ›  技术社区  ›  Sam å±±

为什么这个javascript要插入两个微调器图像?

  •  1
  • Sam å±±  · 技术社区  · 14 年前
    // javascript for ajax pagination//
    
    document.observe("dom:loaded", function() {
      // the element in which we will observe all clicks and capture
      // ones originating from pagination links
      var container = $(document.body)
    
      if (container) {
        var img = new Image
        img.src = '/images/spinner.gif'
    
        function createSpinner() {
          return new Element('img', { src: img.src, 'class': 'spinner' })
        }
    
        container.observe('click', function(e) {
          var el = e.element()
          if (el.match('.pagination a')) {
            el.up('.pagination').insert(createSpinner())
            new Ajax.Request(el.href, { method: 'get' })
            e.stop()
          }
        })
     }
    })
    

    这是html

     <div style="margin-top:25px;">
          <div class="pagination ajax">
               <span class="disabled prev_page">&laquo; Previous</span> 
               <span class="current">1</span> 
               <a href="/posts?page=2" rel="next">2</a> 
               <a href="/posts?page=3">3</a> 
               <a href="/posts?page=2" class="next_page" rel="next">Next &raquo;</a>
          </div>
          <br style="clear:both;"/>
     </div>
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   some    14 年前

    我在jslint中运行javascript并修复了所有错误,但是 new Ajax . 我移动了 createSpinner -函数,因为不建议在块内定义它。同时声明 img

    document.observe(
      "dom:loaded",
      function() {
        var img;
        function createSpinner() {
          return new Element('img', { src: img.src, 'class': 'spinner' });
        }
    
        // the element in which we will observe all clicks and capture
        // ones originating from pagination links
        var container = $(document.body);
    
        if (container) {
          img = new Image( );
          img.src = '/images/spinner.gif';
    
          container.observe(
            'click',
            function(e) {
              var el = e.element();
              if (el.match('.pagination a')) {
                el.up('.pagination').insert(createSpinner());
                new Ajax.Request(el.href, { method: 'get' });
                e.stop();
              }
            }
          );
        }
      }
    );
    
        2
  •  1
  •   morganpdx    14 年前

    是否有两个类=分页的元素?

        3
  •  1
  •   Patrick Ferreira    14 年前

    尝试执行以下更改:

    观察(“dom:loaded”,function(){ //我们将在其中观察所有单击和捕获的元素 var容器=$(document.body)

    var img=新图像 img.src='/images/spinner.gif'

    var createSpinner() = function { 
      return new Element('img', { src: img.src, 'class': 'spinner' });
    }
    
    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {
        el.up('.pagination').insert(createSpinner);
        new Ajax.Request(el.href, { 
          method: 'get',
          success: function(){ el.up('.pagination').find('img.spinner').remove(); }
        });
        e.stop()
      }
    });
    

    });