简体   繁体   English

<div> onClick运行javascript函数不起作用

[英]<div> onClick run javascript function not working

I have the following html code 我有以下html代码

<div id="userMenu">
  <ul>
    <li id="tab1">
      <div onclick="displayTab(1);" class="tab">Transaction History</div>
    </li>
    <li id="tab2">
      <div onClick="displayTab(2)" class="tab">Top Up</div>
    </li>
    <li id="tab3">
      <div onclick="displayTab(3);" class="tab">Profile</div>
    </li>
  </ul>
</div>
<div style="clear: both; margin: 15px; border: 2px solid #99ffff;">
  <div id="content1" class="tabContent">
    Content1
  </div>
  <div id="content2" class="tabContent" style="display: none;">
    Content2
  </div>
  <div id="content3" class="tabContent" style="display: none;">
    Content3
  </div>
</div>

javascript javascript

function displayTab(tabNo){ alert(tabNo);
  for(i = 1; i < 4; i++){
    document.getElementById('content'+i).style.display = 'none';
  } 
  document.getElementById('content'+tabNo).style.display= 'block';
}

Why is it that the function displayTab cannot be activated? 为什么displayTab功能无法激活? If I change the onclick with onclick="alert('1')" , it worked. 如果我使用onclick="alert('1')"更改onclick,则可以正常工作。 I tried on Opera and Firefox, both resulting the same. 我在Opera和Firefox上尝试过,结果都一样。 Error console tells me that there's a 错误控制台告诉我有一个

ReferenceError: Undefined variable displayTab ReferenceError:未定义的变量displayTab

Do note that that is not the full code. 请注意,这不是完整的代码。 Even crazier fact is that if I use that code (the one I put here) as is, it worked and no error in Error Console. 甚至更疯狂的事实是,如果我按原样使用该代码(我在此处输入的代码),则可以正常工作,并且在错误控制台中没有错误。 Help, anyone? 帮忙,有人吗?

尝试使用

onClick="javascript:displayTab(1)" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM