jQuery的:如何找到它是当前元素正在添加前2个元素的元素 [英] jquery: how to find an element which is comming 2 elements before current element

查看:124
本文介绍了jQuery的:如何找到它是当前元素正在添加前2个元素的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标记,看起来是这样的:

i have a markup which look like this:

<h3>Paragraf3-dummytext</h3>
<p>
<a name="paragraf3">
Quisque id odio. Praesent venenatis metus at tortor pulvinar varius. Lorem ipsum dolor sit 
</a>
</p>



我想要做的是找到所有'A'与'名称'属性标签,并找到对于锚H3标签;即时通讯试图做这样的:

what i want to do is to find all 'a' tags with 'name' attribute and find the 'h3' tag for that anchor; im trying to do it like this:

var paragraf = [];
var paragrafheading = [];
$('a[name]').each(function() {
paragraf.push($(this).attr('name'));
paragrafheading.push($(this).prev().text());

但它不工作becouse有围绕着文本的p标签任何建议,将不胜感激谢谢

but it does not work becouse there is a 'p' tag around the text. Any suggestions would be appreciated. Thanks

推荐答案

您可以做的:

paragrafheading.push($(this).parent().prev().text());

如果总有不在身边的一个段落,或者你不知道锚多少家长可以H3的面前,你可以做一些事情是这样的:

If there's not always paragraph around the a, or you don't know how many parents the anchor can have before the h3, you can do something like this:

paragrafheading.push($(this).closest('> h3').find('> h3').text());

这篇关于jQuery的:如何找到它是当前元素正在添加前2个元素的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆