Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的javascript不能为所有人工作?_Javascript_Jquery_Html_Tags - Fatal编程技术网

为什么我的javascript不能为所有人工作?

为什么我的javascript不能为所有人工作?,javascript,jquery,html,tags,Javascript,Jquery,Html,Tags,您好,目前我编写这个java脚本是为了能够显示和隐藏所选选项卡中的内容。但是当我尝试创建另一个UL标签时,我的脚本只适用于我的第一个UL标签,而不适用于第二个UL标签。有人能帮我吗?下面是我的java脚本 var tabLinks = new Array(); var contentDivs = new Array(); function init() { // Assign onclick events to the tab links, and // highlight the

您好,目前我编写这个java脚本是为了能够显示和隐藏所选选项卡中的内容。但是当我尝试创建另一个UL标签时,我的脚本只适用于我的第一个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 );
}
var tabLinks=new Array();
var contentDivs=新数组();
函数init(){
//将onclick事件分配给选项卡链接,以及
//突出显示第一个选项卡
var i=0;
for(tabLinks中的变量id){
tabLinks[id].onclick=showTab;
tabLinks[id].onfocus=function(){this.blur()};
如果(i==0)tabLinks[id].className='selected';
i++;
}
//隐藏除第一个内容div之外的所有内容div
var i=0;
for(contentDivs中的变量id){
如果(i!=0)contentDivs[id].className='tabContent hide';
i++;
}
}
函数showTab(){
var selectedId=getHash(this.getAttribute('href');
//高亮显示所选选项卡,并暗显所有其他选项卡。
//还显示选定的内容div,并隐藏所有其他内容。
for(contentDivs中的变量id){
如果(id==selectedId){
tabLinks[id]。类名='selected';
contentDivs[id].className='tabContent';
}否则{
tabLinks[id]。类名=“”;
contentDivs[id].className='tabContent hide';
}
}
//停止链接后的浏览器
返回false;
}
函数getFirstChildWithTagName(元素,标记名){
对于(var i=0;i
如果我理解您的意图,您希望使用类似的


$(“ul”)//猜测一下,不要使用
getFirstChildWithTagName
。提示:不要将
用于..在
循环数组中,将常规的
用于
循环。也许你想要一个对象?你知道如何使用控制台吗?如果没有:右键单击“inspect element”,然后单击console选项卡查看js错误消息。至少在chrome中是这样,在其他浏览器中应该是类似的。@ElliottFrisch除了getFirstChildWithTagName之外,我还可以使用什么?@elclars我用于..in来获取对象的属性
$( "ul" ) // <-- what you do with it, I can't say - I'd need to 
          //     see more of your code.