我使用CSS和Javascript在我的页面中有图像缩略图标签。当我加载页面时,它只显示选项卡,但不显示相对选项卡中的内容。请找到以下代码并帮助我。
使用Javascript:
<script>
function openCity(evt, cityName) {
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 < tabcontent.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
答案 0 :(得分:0)
我基本上在你的代码中看到了两个拼写错误......
真的 吃 ,或者你输错了,应该是evt?
function openCity(eat, cityName) {
并且基本上此时导致tablinks变量未被使用,这可能会破坏您的代码
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tabcontent.length; i++) {
所以我纠正了,你可以再试一次,就像这段代码LGTM
<script>
function openCity(evt, cityName) {
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(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
如果您不需要支持+ IE8,可以通过map方法替换它 - http://caniuse.com/#feat=es5