【问题标题】:jquery touchwipe how to get $(this)jquery touchwipe 如何获取 $(this)
【发布时间】:2013-01-08 02:33:44
【问题描述】:

我在滚动列表中使用 jquery touchwipe 插件,但无法获取
来自 $(this) 的属性。我想使用 $(this) 来获取其子元素 class="t7 edit" 并向其添加类 'show'。有人知道如何修复它吗?

html:

                <div id="main_list_wrapper">
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                </div>

脚本代码:

var $main_list_wrapper = $("#main_list_wrapper").find('.item');

$main_list_wrapper.touchwipe({
    preventDefaultEvents: false,
    wipeLeft: function() { 
        $(this).find('.t8.cancel').removeClass('show');
        $(this).find('.t7.edit').removeClass('show');
        var thisclass = $(this).attr('class');
        alert(thisclass);
        return false;
    },
    wipeRight: function() { 
        $sb(this).find('.t8.cancel').addClass('show');
        $sb(this).find('.t7.edit').addClass('show');
        return false;
    }
});

喜欢警报(此类)。它显示“未定义”。


谢谢大家。我的朋友写这个给我,它成功了!

$main_list_wrapper.each(function () {
    var $this = $sb(this);

    $this.touchwipe({
        preventDefaultEvents: false,
        wipeLeft: function() { 
            var $pcs = $this;
            $pcs.find('.t8').removeClass('show');
            $pcs.find('.t7').removeClass('show');
            return false;
        },
        wipeRight: function() { 
            var $pcs = $this;
            $pcs.find('.t8').addClass('show');
            $pcs.find('.t7').addClass('show');
            return false;
        }
    });
});

【问题讨论】:

  • “我对$(this)一无所知”是什么意思?请详细说明。如果您要问$(this) 是什么,我认为在这种情况下它将是 $main_list_wrapper。
  • 我想使用 $(this) 来获取它的子元素 class="t7 edit" 并为其添加类 'show'。谢谢。
  • this 可能指的是函数wipeLeft()?
  • 我看到了这篇文章,感谢你写以上内容的朋友,我已经设法让我的刷卡工作了!非常感谢:)

标签: javascript jquery web-applications mobile touch


【解决方案1】:

$(this) 指的是function() 所属的对象,而不是指你的$main_list_wrapper

参考这个post

你可以改用$main_list_wrapper.find('.t8.cancel').removeClass('show');

【讨论】:

    【解决方案2】:

    嗯,$("#main_list_wrapper")NULL。确保这段代码的 sn-p 位于您的 JQuery onLoad 中:$(document).ready(function () { /* CODE */ });

    find() 采用逗号分隔的类名列表。
    将:$(this).find('.t8.cancel') 更改为:$(this).find('.t8, .cancel')

    【讨论】:

    • 是的,这个sn-p的代码位于我的JQuery onLoad中: $(document).ready(function() { /* CODE */ });最大的问题是我不能从 $(this) 获取属性或获取其子元素。谢谢。
    • 好吧,@dunli 之前说过 touchwipe 会包裹一个对象。您可能只需要本机 Javascript 即可访问此内部对象:var element = document.getElementById('main_list_wrapper').children[0]; 如果您想坚持使用 JQuery,这也可能有效:element.children(":first");
    猜你喜欢
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2011-04-25
    相关资源
    最近更新 更多