我有一个 jQuery Mobile Accordion 菜单,设置如下:
<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-->
这适用于几个子类别。我有一些代码可以在单击时从图像中获取子类别名称和标题属性。
var currCat=$(this).closest('.subCat').children('h3').text();
var titleData=$(this).closest('.subcat').children('h3').attr("title");
“this”是被单击的图像。 currCat 获得了它需要的正确字符串,但我总是得到 titleData 的“未定义”。不起诉获得头衔有什么问题。
最佳答案
您的第二行有 subcat
而不是 subCat
。以下内容应该有效:
$(this).closest('.subCat').children('h3').attr("title");
感谢您提醒我选择器中的类名区分大小写。很容易错过的事情。
关于jquery - 使用 jQuery 获取标题属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10490162/