使用 jquery 查找具有特定属性的锚点
我试图在具有特定属性的 div 内找到一个锚点,但 jquery 代码是为了查看锚点的 hreef 值,而不是锚点本身。这是我正在使用的代码:
<script type="text/javascript">
$(document).ready(function() {
$("#ax_campaign_nav a").each(function (i) {
if (this.attr('data-ident') == "ax_models") {
this.hide();
}
});
});
</script>
我的链接如下所示:
<a href="javascript:void(0);" data-ident="ax_models" onclick="scrollToAnchor('#a_ax_models')">+ Model Bios</a>
我做错了什么?感谢您的帮助。
I'm trying to find an anchor inside a div that posses a specific attribute, but the jquery code is for some looking at the hreef value of the anchor, not the anchor itself. This is the code I'm using:
<script type="text/javascript">
$(document).ready(function() {
$("#ax_campaign_nav a").each(function (i) {
if (this.attr('data-ident') == "ax_models") {
this.hide();
}
});
});
</script>
And my link looks like this:
<a href="javascript:void(0);" data-ident="ax_models" onclick="scrollToAnchor('#a_ax_models')">+ Model Bios</a>
What am I doing wrong? Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这样的事情:
Try something like this:
$(this).attr("data-ident")
和$(this).hide()
$(this).attr("data-ident")
and$(this).hide()
如果您正在尝试获取 data-indent 属性,您可能需要检查一下:
http://api.jquery .com/data/
.data() 是 jQuery 1.4.3 中添加的新功能。它非常好,允许您很好地使用 data-key="value" 属性。
另外请务必使用“===”,因为它会检查类型和值。大多数时候,除了极少数特定场景外,您应该始终使用“===”和“!==”。
If your are trying to get the data-indent attribute you probably want to check this out:
http://api.jquery.com/data/
.data() was a new feature added in 1.4.3 of jQuery. It is very nice and allows you to use the data-key="value" attributes quite nicely.
Also be sure to use '===' because it checks for the type as well as the value. Most of the time you should always use '===' and '!==' except for a very few specific scenarios.
无非是:
Nothing more than: