2012-03-04 65 views
0

我有一个搜索框,其中包含两个选项卡以选择要搜索的类型。我想添加.active类来列出其search_type等于来自请求的参数(即,params [request_type])的项目。使用jQuery从HTML元素获取标记

<ul data-tabs="tabs" class="tabs"> 
    <li search_type="Things"> 
    <a href="#Things">Things</a> 
    </li> 
    <li search_type="People"> 
    <a href="#People">People</a> 
    </li> 
</ul> 

我试图jQuery来比较:

$('.tabs .li).(function(){ 
if($(this).search_type == myParams){ 
    $(this).addClass('.active'); 
    } 
}); 

但是,我的$(本).search_type真可谓是不确定的,即使在网络浏览器中,元素正确显示为:

<li search_type="Things"> 
    <a href="#Things">Things</a> 
</li> 

我的问题是:如何从li获取search_type?

谢谢。

回答

4
$('.tabs .li').(function(){ 
if($(this).attr('search_type') == myParams){ 
    $(this).addClass('active'); 
    } 
}); 

通知的$(this).attr()并在addClass不要把. ...只有类的名称。

2

它应该是$(this).attr('search_type')

2

您不能直接引用search_type,请尝试$(this).attr('search_type')并查看发生了什么