0

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

jsfiddle

HTML 代码:

<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>

jQuery代码:

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;
}
4

1 回答 1

0

你的小提琴奏效。只需在Frameworks & Extensions部分的左侧选择No Wrap - in (head)选项。

于 2013-10-08T10:29:00.237 回答