2012-04-02 33 views
1

HTMLjQuery的.parent()来获得一个ID

<p href="products.php?ref=fijo&tipo=7001ad" class="principal">Fijas</p> 
     <div class="menu_body" id="fijo"> 
     <a href="products.php?ref=fijo&tipo=7001ad">Normal (7001AD)</a> 
     <a href="products.php?ref=fijo&tipo=7001md">Aislada (7001MD)</a> 
     <a href="products.php?ref=fijo&tipo=7001ad80">A+ (7001AD80)</a> 
     <a href="products.php?ref=fijo&tipo=7001md80">A+++ (7001MD80)</a> 
     </div> 

JQ

if(cookie = null) 
    { 
    $("div.menu_body a").click(function(){ 
     $.cookie("current",(this).parent().attr("id")); 
    }); 
    } 

我不能说我指向的元素的ID。我不知道这个错误是否与父()父()部分或其他。

我在做一个选项卡系统,我想检查一下是否打开一个选项卡,如果没有任何cookie,如果打开这个选项卡,有人点击该选项卡中的链接,然后存储ID的ID。 menu_body DIV将包含该链接。

+3

创建一个jQuery对象,应该说是'饼干== null'? :○ – 2012-04-02 10:38:20

回答

1

我会说你忘了char

$.cookie("current",(this).parent().attr("id")); 

应该看起来像这样

$.cookie("current",$(this).parent().attr("id")); 

您需要从“此”对象

1

(this).parent().attr("id")不正确,因为this是一个DOM元素。试着用替换它:

jQuery(this).parent().attr("id") 

(你可以用$快捷更换jQuery

在结束你的代码可能类似于此:

if(cookie = null){ 
    $("div.menu_body a").click(function(){ 
     $.cookie("current", $(this).parent().attr("id")); 
    }); 
}