jquery中的selector – 获取rel属性
我有这样的HTML代码:
// something here // something here // something here
我尝试通过此代码获取rel属性:
$('#vol').click(function(){ var rel = $(this).attr('rel'); });
但它不起作用。 我也将this
改为#vol
,但它没有用。 我该怎么办?
通过不工作,我假设你点击任何li
,给你回1
,这是由于页面中有多个ID引起的。 ID应该是唯一的,而是使用类。
尝试
// something here // something here // something here JS: $('.vol').click(function(){ var rel = $(this).prop('rel'); });
你的所有id
都是一样的。所以他们不同。 假设你的li
在ul
里面,哪个类是clsTest
$('.clsTest li').click(function(){ var rel = $(this).attr('rel'); alert(rel); });
选择器按“id”(#id)只选择一个元素,改为使用“class”(。class)
将您的代码更改为
// something here // something here // something here