gpt4 book ai didi

javascript - .prev() 方法不起作用

转载 作者:行者123 更新时间:2023-11-30 10:41:56 27 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
jquery find closest previous sibling with class

下面给出的代码运行良好

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div><span>Hello</span></div>

<p class="selected">Hello Again</p>
<p>And Again</p>
<script>$("p").prev(".selected").css("background", "yellow");</script>

</body>
</html>

但是当我将 .selected 移动到顶部(如下所示)时,它就不起作用了。谁能告诉我如何让它为我工作,并在第二种情况下从 .prev() 方法中获取 .selected 元素无需操作 html

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<p class="selected">Hello Again</p>//Moved to top
<div><span>Hello</span></div>

<p>And Again</p>
<script>$("p").prev(".selected").css("background", "yellow");</script>

</body>
</html>

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com