jQuery通过$(this)遍历HTML
jQuery traversing HTML through $(this)
我有一个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 -->
我有下面的代码来遍历它
$(":jqmData(role='page')").each(function() {
$(this).bind("swipeleft" goLeft); //goLeft and goRight are defined and working
$(this).bind("swipeleft", goRight);
//...
}
滑动工作正常,但我想通过click
行为绑定到next-section
和prev-section
,以调用goLeft
和goRight
,但我不知道如何通过$(this)
对象访问它们。有人知道怎么找到他们吗?
感谢
为它们使用不同的选择器:
$(this).find("#prev-section").click(goLeft);
$(this).find("#next-section").click(goRight);
请注意,您将有多个具有相同ID的元素(#prev部分,#next部分),但ID在整个DOM上应该是唯一的。将其替换为类或数据短划线属性(如用于role=page
的属性)。
$('#next-section').bind('click', goRight);
$('#prev-section').bind('click', goLeft);
或
$('a.next-section').bind('click', goRight);
$('a.prev-section').bind('click', goLeft);
或
$(this).children('#next-section').bind("swipeleft" goLeft);
$(this).children('#prev-section').bind("swipeleft", goRight);
或
$(this).find('#next-section').bind("swipeleft" goLeft);
$(this).find('#prev-section').bind("swipeleft", goRight);
相关文章:
- JS/jquery:我将如何遍历HTML中的每个图像
- 遍历HTML结构以搜索属性
- 使用jquery循环遍历html页面的所有锚点标记
- JS/jQuery遍历HTML元素以动态构建字符串
- 遍历 html 单元格并在满足条件时执行函数
- 如何在 JavaScript 中循环遍历 HTML 行并在满足条件时更改背景
- 循环遍历 HTML 内容并将值作为全局变量返回
- Javascript:仅遍历HTML DOM元素
- 遍历 html 模板中的数组字段
- 使用jQuery遍历HTML
- jQuery通过$(this)遍历HTML
- 使用jQuery遍历html表
- 遍历 HTML DOM 并获得深度
- 如何遍历HTML节点以查找匹配项并从节点中获取下一个innerHTML
- 如何遍历HTML
- 使用jQuery遍历HTML字符串中的classname
- 遍历HTML页面元素
- jQuery在一个变量中遍历HTML并删除第一个单选按钮
- 如何遍历HTML文档中的XML
- 循环遍历HTML表中的元素