因此,当我单击某个<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 (这是一个术语表im构建):
<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>发布于 2013-05-21 09:38:34
您错误地使用了.first()。如果If语句是正确的,则:
$(selectedGloss).children(".glossNav").first().click();将在作为selectedGloss选择器的子类的第一个glossNav类上触发单击事件,而不是其中的第一个锚点。您想要使用它来选择第一个锚,以及从多层深度中选择glossNav。
if ($(selectedGloss + ">.glossNav").length > 0) {
$(selectedGloss + ">.glossNav a").first().click();
}https://stackoverflow.com/questions/16660778
复制相似问题