我的HTML看起来像这样
<div id="1053906-cdm-contract-with-city-of-new-orleans-2013-fema" class="contract-container">
<p class="contract-title contract">CDM- Contract with City of New Orleans: 2013-FEMA-3BCD COOPER GT TOWN DIXON CDM SMITH</p>
<p class="contract-description contract">2013-FEMA-3BCD COOPER GT TOWN DIXON CDM SMITH</p>
<div class="mention-text contract"><div class="page">Page 1</div> sometext </div> <br><br>
<div class="mention-text contract"><div class="page">Page 16</div> some text</div> <br><br>
</div>
当用户点击最外层div中的任意位置时,我想找到最近的“页面”。我用这个jquery
firsthtml = $(this).closest(".page").html();
这会为firsthtml
如果我摆脱.html()
并将鼠标悬停在firsthtml var上,我会看到它返回整个div的HTML。换句话说,它返回带有class="page"
的多个div。
为什么不用“页面”来拉动第一堂课?
答案 0 :(得分:1)
因此.closest()
和.find()
之间存在差异以及您要做的事情。
http://api.jquery.com/closest/
closest
和find
在DOM树中上下导航。如果您想获得.page
的HTML,则必须说出类似
$(this).find('.page').html();
因为`.page'几乎是div结构中的最后一个元素。
如果您想获取FIRST .page
元素的HTML,那就不同了。你不得不说:
$('.page').eq(0).html()
.eq()
是另一种表达.index()
的方式,但它会选择您想要的任何元素。如果要在该特定div中选择该页面,则可以执行
$(this).find('.page').eq(0).html();