为什么警报在 JS 中不起作用,索引返回 -1

why alert not working in js and index return -1?

本文关键字:索引 返回 不起作用 JS 为什么      更新时间:2023-09-26

为什么警报在 js 中不起作用,索引返回 -1?

斯菲德尔

网页代码:

<ul id="menu-controls">
  <li>
    <a target="_self" onclick="return displaySubMenu(0);" id="menu-controls-0" href="#" class="">
      <span>
        Home
      </span>
    </a>
  </li>
  <li>
    <a target="_self" onclick="return displaySubMenu(1);" id="menu-controls-1" href="#" class="">
      <span>
        Oleg Test
      </span>
    </a>
  </li>
  <li>
    <a target="_self" onclick="return displaySubMenu(2);" id="menu-controls-2" href="#" class="act">
      <span>
        Products
      </span>
    </a>
  </li> 
</ul>

J查询代码 :

function displaySubMenu(menuid) {
    var myActive = jQuery("a.act").parent().index();
    var myClicked = jQuery("#menu-controls-"+menuid).parent().index();
    alert(myActive+"==="+myClicked);
    if (myActive < myClicked) {
        alert("Previous active was left");
    } else if (myActive > myClicked) {
        alert("Previous active was right");
    } else if (myActive == myClicked) {
        alert("Previous active is this clicked one");
    }
    jQuery("a.act").removeClass("act");
    jQuery("#menu-controls-"+menuid).addClass("act");
    return false;
}

你的小提琴有效。只需选择左侧"框架和扩展"部分下的"无换行"(头部)选项。