jQuery中的选择器-获取rel属性 [英] selector in jquery - get rel attribute

查看:337
本文介绍了jQuery中的选择器-获取rel属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的html代码:

I have html code like this:

<li id="vol" rel="1">
    // something here
</li>
<li id="vol" rel="2">
    // something here
</li>
<li id="vol" rel="3">
    // something here
</li>

我试图通过以下代码获取rel属性:

I'm try to get rel attribute by this code:

$('#vol').click(function(){
  var rel = $(this).attr('rel');
});

但是它不起作用.我也将this更改为#vol,但是没有用. 我该怎么办?

But it's not working. I also change this to #vol, but it didn't work. What should i do?

推荐答案

由于无法正常工作,我假设您单击的任何li都会带回1,这是由于页面中有多个ID引起的. ID应该是唯一的,请改用class.

By Not working, I'm assuming that any li you click, gives you back 1, this is caused by having multiple ID in a page. IDs are supposed to be unique, use class instead.

尝试

<li class="vol" rel="1">
    // something here
</li>
<li class="vol" rel="2">
    // something here
</li>
<li class="vol" rel="3">
    // something here
</li>

JS:

$('.vol').click(function(){
  var rel = $(this).prop('rel');
});

这篇关于jQuery中的选择器-获取rel属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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