使用jQuery获取title属性

时间:2012-05-07 22:45:11

标签: jquery jquery-mobile

我有一个jQuery Mobile手风琴菜单设置如下:

<div class="subCat" data-role="collapsible">
    <h3 title="someTitle">Subcategory Name</h3>
    <div class="itemImg">
        <img alt="Item 1" src="[image location]" /><span>Item 1</span>
    </div>
    <div class="itemImg">
        <img alt="Item 1" src="[image location]" /><span>Item 1</span>
    </div>
</div><!--End of subCat-->

其中有几个子类别。我有一些代码可以在单击时从图像中获取子类别名称和title属性。

var currCat=$(this).closest('.subCat').children('h3').text();
var titleData=$(this).closest('.subcat').children('h3').attr("title");

“这个”是被点击的图像。 currCat获取它需要的正确字符串,但我总是为titleData获取“undefined”。不起诉获得头衔会出现什么问题。

1 个答案:

答案 0 :(得分:22)

您的第二行有subcat而不是subCat。以下应该有效:

$(this).closest('.subCat').children('h3').attr("title");

感谢您提醒我,classNames在选择器中区分大小写。很容易错过。