为什么我的javascript无法正常运行?

时间:2013-12-27 05:18:47

标签: javascript jquery html tags

嗨,目前我编写了这个java脚本,以便能够显示和隐藏所选标签中的内容。但是当我尝试创建另一个UL标签时,我的脚本只适用于我的第一个UL标签而不是第二个。有人可以帮帮我吗?以下是我的java脚本。

var tabLinks = new Array();
var contentDivs = new Array();

function init() {

  // Assign onclick events to the tab links, and
  // highlight the first tab
  var i = 0;

  for ( var id in tabLinks ) {
    tabLinks[id].onclick = showTab;
    tabLinks[id].onfocus = function() { this.blur() };
    if ( i == 0 ) tabLinks[id].className = 'selected';
    i++;
  }

  // Hide all content divs except the first
  var i = 0;

  for ( var id in contentDivs ) {
    if ( i != 0 ) contentDivs[id].className = 'tabContent hide';
    i++;
  }
}

function showTab() {
  var selectedId = getHash( this.getAttribute('href') );

  // Highlight the selected tab, and dim all others.
  // Also show the selected content div, and hide all others.
  for ( var id in contentDivs ) {
    if ( id == selectedId ) {
      tabLinks[id].className = 'selected';
      contentDivs[id].className = 'tabContent';
    } else {
      tabLinks[id].className = '';
      contentDivs[id].className = 'tabContent hide';
    }
  }

  // Stop the browser following the link
  return false;
}

function getFirstChildWithTagName( element, tagName ) {
  for ( var i = 0; i < element.childNodes.length; i++ ) {
    if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
  }
}

function getHash( url ) {
  var hashPos = url.lastIndexOf ( '#' );
  return url.substring( hashPos + 1 );
}

1 个答案:

答案 0 :(得分:0)

如果我理解你要做什么,你想使用Element Selector之类的

$( "ul" ) // <-- what you do with it, I can't say - I'd need to 
          //     see more of your code.