首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jQuery -我找不到合适的孩子

jQuery -我找不到合适的孩子
EN

Stack Overflow用户
提问于 2013-05-21 09:31:41
回答 2查看 55关注 0票数 0

因此,当我单击某个<a>时,我试图在.glossNav中的第一个.words上伪造一个.letter事件。

以下是我尝试过的方法:

代码语言:javascript
运行
复制
$(".letter").click(function(){
    var whichLetter = $(this).children("span").text();
    var selectedGloss = "#glossary-" + whichLetter;

    $(".words").fadeOut(200);
    $('.letter').removeClass('underline');

    $(selectedGloss).delay(200).fadeIn(200);
    $(this).addClass('underline');

      if($(selectedGloss).children(".glossNav").length > 0) // There isn't always a nav.
      {
            alert("Yippee!");
            $(selectedGloss).children(".glossNav").first().click();
      }
});

“太棒了!”已成功发出警报。

这是我的导航HTML (这是一个术语表im构建):

代码语言:javascript
运行
复制
<div id="modelglossary"><div class="letterBar">
    <a href="javascript:void(0)" class="letter" id="selector-A">
        <span>A</span>
    </a>
    <a href="javascript:void(0)" class="letter" id="selector-B">
        <span>B</span>
    </a>
    <a href="javascript:void(0)" class="letter" id="selector-C">
        <span>C</span>
    </a>
.... and so on
</div>

下面是HTML的主要部分:

代码语言:javascript
运行
复制
<div class="words" id="glossary-S">
    <div id="s-page-1" class="glossPage">
        <span class="glossBlock">
            <strong>Zedcard</strong> - See <a class="wordRef" href="javascript:void(0)">Composite Card</a>.
        </span>
        <span class="glossBlock">
            <strong>Senior model</strong> - A senior model is a professional model in his 40s/50s/60s. As the average age is constantly rising, the advertisements go back more and more to older models to approach their target group. A senior model often has a good book as they can show a lot of experience or after easily being booked for ads they get publications from the beginning on.
        </span>
        <span class="glossBlock">
            <strong>Set</strong> - This is where the action of shoot takes place usually within a professional studio or within a location. It includes all the elements which make the shoot; for example the lighting, camera, art direction and art directed scenery.
        </span>
        <span class="glossBlock">
            <strong>Shooting</strong> - Shooting in general means the implementation of photo or film shoots.
        </span>
    </div>
    <div id="s-page-2" class="glossPage">
        <span class="glossBlock">
            <strong>Stock Photos</strong> - Stock photography is the supply of photographs licensed for specific uses. It is used to fulfill the needs of creative assignments instead of hiring a photographer. Today, stock images are usually presented in searchable online databases, where they are then purchased and delivered online. Often, they are produced in studios using a wide variety of models posing as professionals, stereotypes, expressing stereotypical emotions and gesticulations or involving pets.
        </span>
        <span class="glossBlock">
            <strong>Stylist</strong> - The stylist is in charge of the outfit of the model and discussing at length with the photographer or director, about theme of the shoot.
        </span>
    </div>
    <div class="glossNav">
        <a href="javascript:void(0)" page="s-page-1">1</a>
        <a href="javascript:void(0)" page="s-page-2">2</a>
    </div>
</div>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-21 09:38:05

我觉得你选择器里丢失的a。因为.glossNav是一个div而不是一个<a>..

像这样:$(selectedGloss).find(".glossNav a").first().click();

您应该使用.find()而不是.children(),因为它只选择直接的子元素,而本例中没有这样的元素。:)

请看我的编辑..

票数 4
EN

Stack Overflow用户

发布于 2013-05-21 09:38:34

您错误地使用了.first()。如果If语句是正确的,则:

代码语言:javascript
运行
复制
$(selectedGloss).children(".glossNav").first().click();

将在作为selectedGloss选择器的子类的第一个glossNav类上触发单击事件,而不是其中的第一个锚点。您想要使用它来选择第一个锚,以及从多层深度中选择glossNav。

代码语言:javascript
运行
复制
if ($(selectedGloss + ">.glossNav").length > 0) {
    $(selectedGloss + ">.glossNav a").first().click();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16660778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档