我使用了一些jQuery隐藏和显示页面上的某些元素。但是,它们在页面加载时都会显示。然后,当您与菜单/选项交互时,它会按预期工作。
我不希望它们同时显示以及我以前使用此代码时都显示,但是我已经添加了
任何帮助将不胜感激。
以下是行为: https://gyazo.com/2798001dacc2daf353d845050872a3a0
这是jQuery:
<script type="text/javascript">
$(document).ready(function () {
$('#menu').on('click', 'a', function (e) {
$('.current').not($(this).closest('li').addClass('current')).removeClass('current');
$('.pbox:visible').hide(600);
$('.pbox[id=' + $(this).attr('data-id') + ']').show(600);
e.preventDefault();
});
});
</script>
这是html:
<ul id="menu" >
<li class="current profile-menu center"><a href="#" class="profile-links" data-id="div1"><h2 class="profile-menu">Info</h2></a></li>
<h2 class="profile-menu" style="padding-left:6px;padding-right:6px;font-size:15px;"><span>|</span></h2>
<li class="profile-menu center"><a href="#" class="profile-links" data-id="div2"><h2 class="profile-menu">Stats</h2></a></li>
</ul>
<div class="pbox" id="div1">
<table class="stat-list">
<tbody>
{% if driver.fullNationality %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Nationality</span>
</th>
<td class="stat-value">{{ driver.fullNationality }}</td>
</tr>
{% endif %}
{% if driver.age %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Age</span>
</th>
<td class="stat-value">{{ driver.age }}</td>
</tr>
{% endif %}
{% if driver.placeofbirth %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Born</span>
</th>
<td class="stat-value">{{ driver.placeofbirth }}</td>
</tr>
{% endif %}
{% if driver.lives %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Lives</span>
</th>
<td class="stat-value">{{ driver.lives }}</td>
</tr>
{% endif %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Height</span>
</th>
<td class="stat-value">5ft 11</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Weight</span>
</th>
<td class="stat-value">175lbs</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Highest race finish</span>
</th>
<td class="stat-value">1 (x64)</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Date of birth</span>
</th>
<td class="stat-value">07/01/1985</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Place of birth</span>
</th>
<td class="stat-value">Stevenage, England</td>
</tr>
</tbody>
</table>
</div>
<div class="pbox" id="div2">
<table class="stat-list">
<tbody>
<tr>
<th scope="row" class="stat-key">
<span class="text">Team</span>
</th>
<td class="stat-value">Mercedes</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Wins</span>
</th>
<td class="stat-value">1</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Podiums</span>
</th>
<td class="stat-value">3</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Points</span>
</th>
<td class="stat-value">2730</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">GP's entered</span>
</th>
<td class="stat-value">11</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Championships</span>
</th>
<td class="stat-value">0</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Highest race finish</span>
</th>
<td class="stat-value">1 (x64)</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Highest grid position</span>
</th>
<td class="stat-value">1</td>
</tr>
</tbody>
</table>
</div>
答案 0 :(得分:3)
这是我的处理方式:
$('#menu').on('click', '.profile-tab:not(.current)', function (e) {
$('.profile-menu').removeClass('current');
$(this).addClass("current");
$('.pbox:visible').hide(600);
$('.pbox[id=' + $(this).find('.profile-links').attr('data-id') + ']').show(600);
e.preventDefault();
});
.profile-menu {
display:inline-block;
}
.profile-tab a h2 {
background-color:lightgray;
border-radius:10px;
padding:3px 5px;
}
.profile-tab.current a h2 {
background-color:#005dff;
color:white
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="menu" >
<li class="profile-tab profile-menu current center">
<a href="#" class="profile-links" data-id="div1"><h2 class="profile-menu">Info</h2></a>
</li>
<h2 class="profile-menu" style="padding-left:6px;padding-right:6px;font-size:15px;"><span>|</span></h2>
<li class="profile-tab profile-menu center">
<a href="#" class="profile-links" data-id="div2"><h2 class="profile-menu">Stats</h2></a>
</li>
</ul>
<div class="pbox" id="div1">
<table class="stat-list">
<tbody>
{% if driver.fullNationality %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Nationality</span>
</th>
<td class="stat-value">{{ driver.fullNationality }}</td>
</tr>
{% endif %}
{% if driver.age %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Age</span>
</th>
<td class="stat-value">{{ driver.age }}</td>
</tr>
{% endif %}
{% if driver.placeofbirth %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Born</span>
</th>
<td class="stat-value">{{ driver.placeofbirth }}</td>
</tr>
{% endif %}
{% if driver.lives %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Lives</span>
</th>
<td class="stat-value">{{ driver.lives }}</td>
</tr>
{% endif %}
<tr>
<th scope="row" class="stat-key">
<span class="text">Height</span>
</th>
<td class="stat-value">5ft 11</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Weight</span>
</th>
<td class="stat-value">175lbs</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Highest race finish</span>
</th>
<td class="stat-value">1 (x64)</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Date of birth</span>
</th>
<td class="stat-value">07/01/1985</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Place of birth</span>
</th>
<td class="stat-value">Stevenage, England</td>
</tr>
</tbody>
</table>
</div>
<div class="pbox" id="div2" style="display:none">
<table class="stat-list">
<tbody>
<tr>
<th scope="row" class="stat-key">
<span class="text">Team</span>
</th>
<td class="stat-value">Mercedes</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Wins</span>
</th>
<td class="stat-value">1</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Podiums</span>
</th>
<td class="stat-value">3</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Points</span>
</th>
<td class="stat-value">2730</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">GP's entered</span>
</th>
<td class="stat-value">11</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Championships</span>
</th>
<td class="stat-value">0</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Highest race finish</span>
</th>
<td class="stat-value">1 (x64)</td>
</tr>
<tr>
<th scope="row" class="stat-key">
<span class="text">Highest grid position</span>
</th>
<td class="stat-value">1</td>
</tr>
</tbody>
</table>
</div>