繁体   English   中英

将类添加到HTML元素

[英]Add class to HTML element

我想使用<a href="http://localhost/hello/5">元素将类添加到<li>元素。

我尝试过,但是不起作用:

$("<li><a>[href|='http://localhost/hello/5']").addClass( "active" );

要添加到父<li>

$("[href='http://localhost/hello/5']").parent("li").addClass( "active" );

从选择器中删除<> 这将创建元素,而不是选择它们。 由于要将类添加到带有href的a的li中,因此需要选择链接,然后将DOM上移至li。 这样就可以将类添加到正确的元素。 它应该是:

$("a[href='http://localhost/hello/5']").parent("li").addClass( "active" );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM