将现有的jQuery函数绑定到链接

时间:2019-01-28 19:25:32

标签: javascript jquery

我试图通过单击触发现有的jQuery函数。有人告诉我,可以通过 .bind http://api.jquery.com/bind/

完成

我正在使用下面的代码来刷过变形图像:

a )向左滑动=我不喜欢

b )向右滑动=我喜欢

我希望能够通过单击链接进行完全相同的操作。

*。js代码:

jQuery(function($){

    var likedCount = 0;

    $('.postswiper-post').live('swiperight',function(){
        if ( !$(this).hasClass('rot-left') && !$(this).hasClass('rot-right') ){
            $(this).addClass('rot-left');
            $('.postswiper-post').find('.status').remove();

            $(this).append('<div class="status like">Like!</div>');
            likedCount++;

            var postTitle = $(this).find('.postswiper-posttitle').html();
            $('.postswiper-likedlist-list').append('<div class="postswiper-likedlist-liked">' + postTitle + '</div>');
            $('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');

            if ( $('.postswiper-likedlist-opener').hasClass('disabled') ) {
                $('.postswiper-likedlist-opener').removeClass('disabled');
            }

            if ($(this).is(':last-child')) {
                $('.postswiper-wrapper > p').css('color','inherit');
            }
        }
    });

    //

jQuery:https://gist.github.com/edoardoo/89e672f22fa405799fdd

  

/ *! jQuery Mobile v1.4.5 |版权所有2010、2014 jQuery Foundation,   公司| jquery.org/license * /

我在页面上添加了以下html代码:


<div class="colnew"><a class="click-right" href="/" onclick="return false;">LOVE IT!</a></div>

和* .js文件:

jQuery(function($){

    var likedCount = 0;

    $('.postswiper-post').live('swiperight',function(){
        if ( !$(this).hasClass('rot-left') && !$(this).hasClass('rot-right') ){
            $(this).addClass('rot-left');
            $('.postswiper-post').find('.status').remove();

            $(this).append('<div class="status like">Like!</div>');
            likedCount++;

            var postTitle = $(this).find('.postswiper-posttitle').html();
            $('.postswiper-likedlist-list').append('<div class="postswiper-likedlist-liked">' + postTitle + '</div>');
            $('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');

            if ( $('.postswiper-likedlist-opener').hasClass('disabled') ) {
                $('.postswiper-likedlist-opener').removeClass('disabled');
            }

            if ($(this).is(':last-child')) {
                $('.postswiper-wrapper > p').css('color','inherit');
            }
        }
    });

//my bind code 

    function fBindFunctionToElement(){
   $('.click-right').bind('swiperight',function(){
        if ( !$(this).hasClass('rot-left') && !$(this).hasClass('rot-right') ){
            $(this).addClass('rot-left');
            $('.postswiper-post').find('.status').remove();

            $(this).append('<div class="status like">Like!</div>');
            likedCount++;

            var postTitle = $(this).find('.postswiper-posttitle').html();
            $('.postswiper-likedlist-list').append('<div class="postswiper-likedlist-liked">' + postTitle + '</div>');
            $('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');

            if ( $('.postswiper-likedlist-opener').hasClass('disabled') ) {
                $('.postswiper-likedlist-opener').removeClass('disabled');
            }

            if ($(this).is(':last-child')) {
                $('.postswiper-wrapper > p').css('color','inherit');
            }
        }
    });
(...)

当我单击“ LOVE IT”链接时,它不会触发jQuery函数。

1 。我在做什么错了?

2 。还有其他方法可以通过点击来调用现有的jQuery函数吗?

修改

以下解决方案可以部分解决问题:

$(document).on("click",".click-right",function() { 
if ( !$(this).hasClass('rot-left') && !$(this).hasClass('rot-right') ){
            $(this).addClass('rot-left');
            $('.postswiper-post').find('.status').remove();

            $(this).append('<div class="status like">Like!</div>');
            likedCount++;

            var postTitle = $(this).find('.postswiper-posttitle').html();
            $('.postswiper-likedlist-list').append('<div class="postswiper-likedlist-liked">' + postTitle + '</div>');
            $('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');

            if ( $('.postswiper-likedlist-opener').hasClass('disabled') ) {
                $('.postswiper-likedlist-opener').removeClass('disabled');
            }

            if ($(this).is(':last-child')) {
                $('.postswiper-wrapper > p').css('color','inherit');
            }
        }
    });

当我点击“爱它”时,这部分代码被触发

$('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');

但是我无法像使用鼠标滑动一样重新加载图像。我有种忘记的东西,很明显的笑声

1 个答案:

答案 0 :(得分:-1)

尝试一下(只是WAG ...)

代替:

$('.postswiper-post').live('swiperight',function(){

要做:

$(document).on('swiperight click', '.postswiper-post', function(){

类似地:

   /*  NON-RUNNING CODE SNIPPET -- For Viewing Only  */
   $(document).on('swiperight click', '.click-right'),function(){
        if ( !$(this).hasClass('rot-left') && !$(this).hasClass('rot-right') ){
            $(this).addClass('rot-left');
            $('.postswiper-post').find('.status').remove();

            $(this).append('<div class="status like">Like!</div>');
            likedCount++;

            var postTitle = $(this).find('.postswiper-posttitle').html();
            $('.postswiper-likedlist-list').append('<div class="postswiper-likedlist-liked">' + postTitle + '</div>');
            $('.postswiper-likedlist-count').text('View my liked items (' + likedCount + ')');

            if ( $('.postswiper-likedlist-opener').hasClass('disabled') ) {
                $('.postswiper-likedlist-opener').removeClass('disabled');
            }

            if ($(this).is(':last-child')) {
                $('.postswiper-wrapper > p').css('color','inherit');
            }
        }
    });