jQuery $(document).on(eventName,selector,function(){})不起作用

时间:2016-10-25 12:05:51

标签: javascript jquery

我有一个点击功能,当我在页面加载时隐藏了这些元素时,它会运行,然后再显示它们。现在我在页面加载后点击这些元素,当然功能不起作用。我以为我会用load()函数修复问题,但它仍然不起作用。这是我在点击时附加的html:

$('#magazine-detail')[0].innerHTML = '<section id="magazine-detail" class="magazine-detail"><div class="large-6 medium-6 small-12 columns"><div class="magazine-hero"><img id="image" src="/imagecache/cover/' + magazineImage + '" alt="' + name + '" /><div class="magazine-preview-nav"><div class="right-arrow" id="forward"><img src="/img/right-arrow-black.svg" /><p>Neste utgivelse</p></div><div class="left-arrow" id="back"><img src="/img/left-arrow-black.svg" /><p>Forrige utgivelse</p></div></div></div></div><div class="large-6 medium-6 small-12 columns"><div class="row"><div class="small-6 columns magazine-title"><h1 id="name"></h1></div></div><p id="summary"></p><img id="issueImage" src="" alt="" /><p></p><button class="button primary expand">Kjøp abonnement - 1 måned gratis</button><button class="button secondary expand">Bla igjennom arkivet</button></div></section>';

在那里,我创建了#forward#back元素,我在脚本中点击了这些元素:

$(document).ready(function () {
    var imagesIndex = 0;
    nextImage = 0;
    loadedImages = new Array();

    function preload() {
        for (i = 0; i < 2; i++) {
            if (nextImage < images.length) {
                var img = new Image();
                img.src = '/imagecache/cover/' + images[nextImage];
                loadedImages[nextImage] = img;
                ++nextImage;
            }
        }
    }

    $('#forward').load('click', function() {
      imagesIndex++;
      preload();

      if (imagesIndex > (loadedImages.length - 1)) {
          imagesIndex = loadedImages.length - 1;
      }

      $('#image').attr({"src" : loadedImages[imagesIndex].src, "alt" : name});
    });

    $('#back').load('click', function() {
        imagesIndex--;

        if (imagesIndex < 0) {
            imagesIndex = 0;
        }

        $('#image').attr({"src" : loadedImages[imagesIndex].src, "alt" : name});
    });
});

我尝试过:

$(document).on('click','#forward', function() {
      console.log('entered');
      imagesIndex++;
      preload();

      if (imagesIndex > (loadedImages.length - 1)) {
          imagesIndex = loadedImages.length - 1;
      }

      $('#image').attr({"src" : loadedImages[imagesIndex].src, "alt" : name});
    });

    $(document).on('click','#forward', function() {
        imagesIndex--;

        if (imagesIndex < 0) {
            imagesIndex = 0;
        }

        $('#image').attr({"src" : loadedImages[imagesIndex].src, "alt" : name});
    });

但是点击元素时没有记录任何内容。

2 个答案:

答案 0 :(得分:1)

$user = User::where('name', $findUserInput)->first();

if($user)
{
    $user->name = $request->get('name');

    $user->save();
}

答案 1 :(得分:0)

像这样写

  $(document).on('click','#forward', function() {