Jquery .parent() to get an ID



HTML

<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。我不知道错误是在parent()parent(()部分还是其他部分。

我正在做一个选项卡系统,我想检查是否现在打开了一个选项卡,是否没有任何cookie,是否打开了这个选项卡,并且有人单击了该选项卡中的链接,然后存储包含该链接的.menu_body DIV的ID。

我想说你忘了一个字符

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

应该看起来像这个

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

您需要从"this"对象

创建一个jquery对象

(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"));
    });
}

最新更新