【问题标题】:Trying to find an child image element passed using "this" in jquery试图在 jquery 中查找使用“this”传递的子图像元素
【发布时间】:2011-08-22 18:05:00
【问题描述】:

我正在尝试访问在 jQuery 中使用 this 传递给事件处理程序的图像。

因此,在这种情况下,“this”是一个 div 或 li,在 div 或 li 中包含一个 img 标签。

我尝试了以下访问器:

$(this).attr(), $(this).('.imgClassName').attr(), $(this: img).attr(), $(this.imgClassName).attr()

这些都不起作用。这是我的脚本:

        var closedText = 'Show All';
        var openedText = 'Hide All';
        var helpExpand = '/images/expand.png';
        var helpCollapse = '/images/collapse.png';

$('li.helpList: a').click(function () {
            $(this).next().slideToggle('fast', function () {

                if (faqClosed) {
                    $(this).attr({
                        src: helpExpand,
                        title: openedText,
                        alt: openedText
                    });
                } else {
                    $(this).attr({
                        src: helpCollapse,
                        title: closedText,
                        alt: closedText
                    });
                }
            });
            return false;
        });

这不起作用。但是,这确实有效(对于我的隐藏/显示全部):

$('#showAll').click(function () {
            if (faqClosed) { // closed
                $('li.helpList: div').show('fast'); faqClosed = false;
                $('#showAll').text(openedText);
                $(".helpToggle").attr({
                    src: helpExpand,
                    title: openedText,
                    alt: openedText
                });
            } else { // opened
                $('li.helpList: div').hide('fast'); faqClosed = true;
                $('#showAll').text(closedText);
                $(".helpToggle").attr({
                    src: helpCollapse,
                    title: closedText,
                    alt: closedText
                });
            }
            return false;
        });

$('li.helpList: div') 是打开/关闭项目的容器。其中有一个带有小箭头的 img 标签。这是容器的 HTML:

             <li class="helpList">
                <a href="#">
                    <img src="/images/collapse.png" class="helpToggle" alt="Contraer" />How do I do stuff?
                </a>
                <div class="helpContent">To do stuff, please do something.</div>
            </li>

[更新]

这是我的测试脚本,它根据状态提醒打开或关闭。这目前有效,但仍然找不到图像(collapse/expand.png 文件不会隐藏,并且永远不会改变)

 // Wire up hide/show to a.click event: 
        $('li.helpList: a').click(function () {
            $(this).next().slideToggle('fast', function () {
                if (faqClosed) {
                    alert('closed');
                    $(this).find('.helpToggle').hide();
                    $(this).find('img').attr({
                        src: helpExpand,
                        title: openedText,
                        alt: openedText
                    });

                    faqClosed = false;

                } else {

                    alert('open');
                    $(this).find('.helpToggle').show();
                    $(this).find('img').attr({
                        src: helpCollapse,
                        title: closedText,
                        alt: closedText
                    });

                    faqClosed = true;
                }

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您从a 标记开始。你调用.next() 得到下一个兄弟(div),然后调用slideToggle()

    this 在该上下文中是 div,它不包含图像。

    如果您想在a 标记内搜索,请在调用slideToggle() 之前存储对它的引用,然后将其用作选择器的上下文(第二个参数)。

    $('li.helpList: a').click(function () {
         var anchor = this;
         ... // snipped for brevity
         $('img', anchor).attr(...)
    

    这里有一个working demo 来说明它(为图片道歉,这是我唯一可用的)。

    【讨论】:

    • 哈哈哈 - 神奇的狗来救援!!!非常酷 - 我印象深刻。这是一个杀手级的答案谦卑地鞠躬。我明白你在说什么,“this”是 div,直到我调用 next() - 此时它会在函数的整个范围内增加 this 的级别。很有意思。感谢演示 - jsfiddle 吧?我将开始将此站点用于我的 sn-ps。 /高五
    • @Shawn:哈哈!是的,我真的不应该为获得神奇的神奇狗的帮助而道歉!在您的示例中,thisa,直到您调用 next(),它获取下一个兄弟(div)。然后针对next() 返回的对象调用的任何函数都针对匹配的元素运行:在本例中为div
    【解决方案2】:

    你可以使用:

    $(this).find('img')...
    

    获取所有子图像(任何深度)。

    【讨论】:

    • 我刚刚尝试将 $(this).find('img').show() 和 $(this).find('img').hide() 添加到 if/else 语句中(分别) 以便于调试,但它不起作用。它不接触图像。我也用类名试过了。实际上它并没有隐藏任何图像......也许“this”没有范围内的img?我不确定那是怎么回事。还有其他想法吗?
    • 也许我在你的初始代码中错过了这个......这是你的真实代码吗? $('li.helpList: a')我不认为你需要或想要冒号:你想要$('li.helpList a')
    • MMmmmm 我没注意到。这很可能是问题所在。我现在会检查并回复我的结果。谢谢!
    • 这没有帮助。我需要 :a 才能找到被点击的元素。我知道它有效,因为我在每个 if/else 案例中添加了一个“alert()”,它会正确地提醒打开/关闭。在我的 中有一个 标签,所以它应该在“this”中找到 img。我将使用此测试代码更新代码块。
    • 嗯,我需要检查一下我的选择器语法,因为我不知道 ":a" 的作用。
    【解决方案3】:

    你试过 $(this).find('.imgClassName').attr() 吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-28
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多