<ul>
<li onclick="a()">A</li>
<div>
<li>1</li>
<li>2</li>
</div>
<li>B</li>
</ul>
a(){
$(this).next().css("background","red");
}
A下面的div怎么没变成红色呢
问题出在this上,改为下面的代码就可以了:
<script> function a(elm){ console.log(elm); $(elm).next().css("background","red"); } </script> </head> <body> <ul> <li onclick="a(this)">A</li> <div> <li>1</li> <li>2</li> </div> <li>B</li> </ul> </body>