在jQuery中选择兄弟的内容

时间:2012-02-24 18:09:31

标签: jquery html5 attributes fancybox siblings

我正在尝试获取jQuery中某个链接的兄弟的价值,但我不确定如何选择它。这是我的HTML代码。

<figure>
    <a class="figure" href="http://wbond.net/sublime_packages/img/package_control/install_package.png"><img src="http://wbond.net/sublime_packages/img/package_control/install_package.png" title="Something"></a>
    <figcaption>
        Great Picture Description. Original by <a href="http://www.example.com/">Random person</a>
    </figcaption>
</figure>

我想显示figcaptionfigure的内容。我的实际代码是使用链接中的title属性。

$(obj).attr('title')

感谢您的支持

3 个答案:

答案 0 :(得分:0)

http://jsfiddle.net/hVEfC/

var content = $('figure figcaption').html();
alert(content);​

答案 1 :(得分:0)

要回答您的标题,(但不是您的问题),这里是next sibling的选择器:

$('.figure ~ figcaption').

答案 2 :(得分:0)

我假设您要使用figcaption的内容作为fancybox title ....所以上面使用相同的html和fancybox v2.x 试试这个脚本:

<script type="text/javascript">
var newCaption;
$(document).ready(function() {
 $(".figure").fancybox({
  afterLoad: function(){
   newCaption = $(this.element).next('figcaption').html();
   this.title = newCaption
  }
 }); // fancybox
}); // ready
</script>

如果您想更改title类型,请使用helpers选项,例如

helpers: {
 title : {
  type : 'inside'
 }
}