javascript - 使用内联 JavaScript 重构代码

标签 javascript refactoring

我正在尝试重构这个 w3schools 以获得可切换的选项卡代码,因为我们都知道使用内联 JavaScript 是一种非常糟糕的做法,所以我试图尽可能地将它们分开,所以我选择了 tablinks 并且我添加了一个事件监听器,但我正在为城市名称而苦苦挣扎(看看他们的代码,你就会明白我在说什么)

任何帮助请提前谢谢你

HTML

<!-- Tab links -->
<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')">London</button>
  <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
  <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<!-- Tab content -->
<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>

CSS

.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons that are used to open the tab content */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}

JavaScript

function openCity(evt, cityName) {
  // Declare all variables
  var i, tabcontent, tablinks;

  // Get all elements with class="tabcontent" and hide them
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }

  // Get all elements with class="tablinks" and remove the class "active"
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }

  // Show the current tab, and add an "active" class to the button that opened the tab
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}

最佳答案

w3schools 是错误做法的一个很好的来源,因为他们在试图突出他们给出的一个小例子时经常走很多捷径。

在实践中,您可以使用数据属性来解决这种情况。如果您不认识某些调用,请参阅其中一些文章:

// Wrap our code in an IIFE in order to avoid polluting the global namespace
// and to facilitate faster garbage collection
(function(){

// Preload queries for later use
const tabs = document.querySelectorAll('.tablinks');
const content = document.querySelectorAll('.tabcontent');

// iterate tab to create content interaction
tabs.forEach(f => // f will be the tab element in this loop

  // Assign click event to each tab
  f.addEventListener('click',function(){

    // Locate any previously marked active tab element
    const prevActive = document.querySelector('.tablinks.active');

    // If a previously marked element exists set its classname to default
    if(prevActive) prevActive.className = 'tablinks';

    // Assign the currently clicked tab element the active class
    f.className = 'tablinks active';

    // Iterate through the content to look for the data-attribute we used earlier
    content.forEach(c => { // c will be the content element in this loop

      // if the id of the element matches the data attribute from the tab then show the content
      c.style.display = c.id == f.getAttribute("data-city") ? "block" : "none" ;
    })
  })
);

})();
.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons that are used to open the tab content */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
<div class="tab">
  <button class="tablinks" data-city="London">London</button>
  <button class="tablinks" data-city="Paris">Paris</button>
  <button class="tablinks" data-city="Tokyo">Tokyo</button>
</div>

<!-- Tab content -->
<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>

关于javascript - 使用内联 JavaScript 重构代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55582445/

相关文章:

JavaScript 用一组不同的字母替换每个字母,但不重做已经替换的部分

javascript - meteor cfs-s3 错误与 CollectionFS "FS.TempStore.Storage is not set"

javascript - 如何隐藏选项卡的内容

javascript - 如何在 JavaScript 中同步播放音频文件?

c# - 重构相似方法

javascript - 如何在 Javascript 数组中查找单词(字符串)?

c++ - C++ 中特定重构的惯用语

java - 最小化 Java 中的可见性

c# - 跨类/控件调用 - 传递引用、使用事件或更改结构

Java反模式名称?包含对象的对象包含...等等