0

因此.click(),当我单击<a>..glossNav.words.letter

这是我尝试过的:

$(".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(这是我正在构建的词汇表):

<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 的大部分内容:

<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>
4

2 回答 2

4

我认为您a在选择器中丢失了。因为.glossNav是 div 不是<a>..

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

您应该使用.find(),而不是.children()因为它只选择直接子元素,而这不是您的情况.. :)

看我的编辑..

于 2013-05-21T01:38:04.907 回答
0

您使用 .first() 不正确。如果你的 if 语句是正确的,那么:

$(selectedGloss).children(".glossNav").first().click();

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

if ($(selectedGloss + ">.glossNav").length > 0) {
    $(selectedGloss + ">.glossNav a").first().click();
}
于 2013-05-21T01:38:33.543 回答