如果元素不是父元素,我如何向上移动一个元素?

How can I move up by one element if element is not the parent?

基本上我有这样的结构

<ul>
  <li class="menu-parent">
    <ul class="tier-two">...</ul>
  </li>
  <div class="square"></div>


  <li class="menu-parent">
    <ul class="tier-two">...</ul>
  </li>
</ul>

我想要的是有一个事件处理程序,当我单击 div.square 时,它会在其前面显示 ul.tier-two。我曾尝试使用 closest() 但不知何故它不起作用。

如果需要,可以使用 $('.square').prev('li').find('ul.tier-two') 和 jQuery。

希望对您有所帮助。

$("div.square").on("click", function(){
  $(this).next("li.menu-parent").find("ul.tier-two").show();
});