jQuery通过$(this)遍历HTML

Mas*_*iar 0 javascript jquery jquery-mobile

我有一个HTML文档,结构如下:

<section id="page1" data-role="page"> 
    <a href="#" id="next-section" class="next-section" style=" cursor:e-resize"><div class="arearight" alt="go right" ></div></a>
    <a href="#" id="prev-section" class="prev-section" style=" cursor:w-resize"><div class="arealeft" alt="go left" ></div></a>
...   
</section>

<!-- more sections -->
Run Code Online (Sandbox Code Playgroud)

我有以下代码来遍历它

$(":jqmData(role='page')").each(function() {
    $(this).bind("swipeleft" goLeft); //goLeft and goRight are defined and working
    $(this).bind("swipeleft", goRight);
    //...
}
Run Code Online (Sandbox Code Playgroud)

轻扫工作正常,但我想绑定到next-sectionprev-section一个click行为调用goLeftgoRight,但我不知道如何通过访问这些$(this)对象.有没有人知道如何去找他们?

谢谢

Ita*_*tay 5

为它们使用不同的选择器:

$(this).find("#prev-section").click(goLeft);

$(this).find("#next-section").click(goRight);
Run Code Online (Sandbox Code Playgroud)

请注意,您将拥有多个具有相同ID的元素(#prep-section,#next-section),但ID应该在整个DOM上是唯一的.将其替换为类或数据破折号属性(如您正在使用的属性role=page).