【问题标题】:VueJS TypeError: Cannot read property 'className' of undefinedVueJS TypeError:无法读取未定义的属性'className'
【发布时间】:2020-11-08 20:12:45
【问题描述】:

我正在尝试构建一个菜单,突出显示用户所在的当前选项卡/页面,添加一个颜色类,并使用 JavaScript 更新活动链接。这是我的模板:

<div class="w3-bar w3-black">
  <button
    class="w3-bar-item w3-button tablink w3-red"
    onclick="openCity(event,'London')"
  >
    London
  </button>
  <button
    class="w3-bar-item w3-button tablink"
    onclick="openCity(event,'Paris')"
  >
    Paris
  </button>
  <button
    class="w3-bar-item w3-button tablink"
    onclick="openCity(event,'Tokyo')"
  >
    Tokyo
  </button>
</div>

<div id="London" class="w3-container w3-border city">
  <h2>London</h2>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="w3-container w3-border city" style="display:none">
  <h2>Paris</h2>
  <p>Paris is the capital of France.</p>
</div>

<div id="Tokyo" class="w3-container w3-border city" style="display:none">
  <h2>Tokyo</h2>
  <p>Tokyo is the capital of Japan.</p>
</div>

我在“方法”中有这个 javascript 代码:

methods: {
    openCity: function(evt, cityName) {
      var i, x, tablinks;
      x = document.getElementsByClassName("city");
      for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
      }
      tablinks = document.getElementsByClassName("tablink");
      for (i = 0; i < x.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" w3-red", "");
      }
      document.getElementById(cityName).style.display = "block";
      evt.currentTarget.className += " w3-red";
    }
  }

但我在控制台中收到此错误:'TypeError: Cannot read property 'className' of undefined' 并且它没有突出显示它的按钮。 希望您能帮帮我,谢谢!

【问题讨论】:

  • 什么类名未定义? tablinks[i].classNam 还是 evt.currentTarget.className?

标签: javascript css vue.js


【解决方案1】:

您传递的是event 而不是$event,我认为这就是它未定义的原因。你还有很多不必要的代码。我清理了它,现在试试:

HTML

<div class="w3-bar w3-black">
  <button
    v-for="city in cities"
    class="w3-bar-item w3-button tablink"
    @click="openCity($event, city)"
    :class="{ 
      'w3-red': city.name === selectedCity,
      'w3-black': city.name !== selectedCity 
    }"
  >
    {{city.name}}
  </button>
</div>

<div 
  v-for="city in cities" 
  :id="city.name" 
  class="w3-container w3-border city"
  :class="{ 
    'visible': city.name === selectedCity,
    'hidden': city.name !== selectedCity 
  }"
 >
  <h2>{{city.name}}</h2>
  <p>{{city.description}}</p>
</div>

脚本

data () {
  return {
    selectedCity: 'London',
    cities: [
      { 
        name: 'London', 
        description: 'London is the capital city of England'
      },
      { 
        name: 'Paris', 
        description: 'Paris is the capital of France',
      },
      { 
        name: 'Tokyo', 
        description: 'Tokyo is the capital of Japan',
      }
    ] 
  }
},
methods: {
  openCity (event, city) {
    this.selectedCity = city.name
  }
}

样式

.hidden {
  display: none
}

.visible {
  display: block
}

方法二

如果你不想使用 CSS 类来隐藏内容,你可以使用 v-show 并且只会显示选定的城市 city.name === selectedCity

<div 
  v-for="city in cities" 
  v-show="city.name === selectedCity"
  :id="city.name" 
  class="w3-container w3-border city"
 >
  <h2>{{city.name}}</h2>
  <p>{{city.description}}</p>
</div>

【讨论】:

  • @NuzzeSicK 很高兴我能帮上忙 :)。我再次更新了代码,看看,我让它更灵活了。这样,当您拥有更多数据时,使用起来会容易得多。
猜你喜欢
  • 2021-12-08
  • 2017-09-16
  • 2020-08-27
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 2020-11-23
  • 1970-01-01
相关资源
最近更新 更多