动态添加/删除类Jquery

| 我正在创建一个自动建议框,并向其返回建议,并且我试图将类“ searchsuggestinnerulhighlight”添加/删除到各个链接。这是动态返回的ta
    <DIV id=\"searchsuggestinner\">
    <UL id=searchsuggestinnerul>
    <LI>
    <A href=\"#\" id=\"1\" class = \"hoverme\" onMouseDown=\"searchsuggestSubmit(\'appalachian trail\');\">appalachian trail</A>
   </LI>
   </UL>
   </DIV>
这是我的jQuery:
 $(\".hoverme\").live(\"mouseover mouseout\", function(event) {
    if ( event.type == \"mouseover\" ) {
       $(\"#\" + mocount).removeClass(\"searchsuggestinnerulhighlight\");
       mocount = $(this).attr(\'id\');
       $(\"#\" + mocount).addClass(\"searchsuggestinnerulhighlight\");
    } else {
       $(\"#\" + mocount).removeClass(\"searchsuggestinnerulhighlight\");
    }
 });
最初我有.css(\“ background-color \” ... ...现在我已经将其更改为添加类和删除类,但是它不起作用。有什么想法吗?     
已邀请:
更改:
$(\"#\" + mocount).add(\"searchsuggestinnerulhighlight\");
至:
$(\"#\" + mocount).addClass(\"searchsuggestinnerulhighlight\");
    
mocount = $(this).attr(\'id\');
$(\"#\" + mocount)
那真是狡猾的jQuery! 首先,您不需要
attr
就可以得到
id
。你可以用
this.id
买到它。这快得多了。 其次,您无需获取ѭ6即可获取包含clicked元素的jQuery选择。只需改用
$(this)
。 最后,正如Gabe所说,使用use10而不是ѭ11。因此,总而言之:
$(this).addClass(\'searchsuggestinnerulhighlight\');
不过,另一件事-在HTML5之前的HTML中,不允许使用以数字开头的ID值。无法保证其行为。     
我会对此发表评论,但我不能。实现lonesomeday \的更改,但是当他使用$(this).id时,请尝试仅使用this.id,因为\'this \'应该已经是一个jquery对象。 (我们不要$($(this))。)     

要回复问题请先登录注册