如何在javascript中的函数内单击按钮上的调用方法

时间:2018-12-28 23:47:00

标签: javascript

我一直在尝试自学Javascript,并陷入一个特定的问题。我正在尝试为自己创建一个投资组合网站,并希望在该网站上包含可点击的标签。我为这些选项卡编写了以下代码,但无法完全正常工作:

<script>
function TabSet(){
  this.create = function(tabs){
    var menuElement = document.getElementById("menu");
    menuElement.className = "tab";
    for (var i = 0; i < tabs.length;i++){
      var button = document.createElement("button");
      button.className= "tablinks";
      button.innerHTML = tabs[i];
      button.onclick = function(){
        // right here is where i'm struggling.
          TabSet.prototype.call.openTab(this.innerHTML);
      };
      menuElement.appendChild(button);
    }
    this.openTab(tabs[0]);
  };
  this.openTab = function(tabName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
      tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
      tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
     document.getElementById(tabName).style.display = "block";
     event.currentTarget.className += " active";
  }
 }



function createTabSet(){
   var myTabSet = new TabSet();
   var tabs = ["About me","Resume", "Portfolio", "Code", "Contact" ];
  myTabSet.create(tabs);
}



</script>

<body onload="createTabSet()">
<script>
</script>

当我执行此代码时,单击其他选项卡不会改变

据我所知,这段代码应该可以正常工作,而问题出在这里:

      TabSet.prototype.call.openTab(this.innerHTML);

我觉得这里的语法有点错误,有人知道编写此行的正确方法吗?还是其他问题呢?

0 个答案:

没有答案