Javascript JS在定位锚点时隐藏一个div
我有多个默认隐藏的div class=profile。每个div仅在目标时显示。我希望当其中一个配置文件div成为目标时,class=employeeul的所有div都被隐藏。我不知道这是怎么回事,有人知道为什么吗?JS解决方案也不错。我想我不能使用onclick,因为当从其他站点访问锚时,div必须隐藏 这是我的代码,我删除了divs内容:Javascript JS在定位锚点时隐藏一个div,javascript,html,css,Javascript,Html,Css,我有多个默认隐藏的div class=profile。每个div仅在目标时显示。我希望当其中一个配置文件div成为目标时,class=employeeul的所有div都被隐藏。我不知道这是怎么回事,有人知道为什么吗?JS解决方案也不错。我想我不能使用onclick,因为当从其他站点访问锚时,div必须隐藏 这是我的代码,我删除了divs内容: <div class="narrow_content"> <div class="profile" id="m_empf
<div class="narrow_content">
<div class="profile" id="m_empfang0"></div>
<div class="profile" id="m_empfang1"></div>
<div class="profile" id="m_mitarbeiter0"></div>
<div class="profile" id="m_mitarbeiter1"></div>
<div class="profile" id="m_mitarbeiter2"></div>
<div class="profile" id="m_mitarbeiter3"></div>
<div class="profile" id="m_mieter0"></div>
<div class="profile" id="m_mieter1"></div>
<div class="profile" id="m_mieter2"></div>
<div class="employeeul">
<ul> <!-- Empfang -->
<li class="employee"></li>
<li class="employee"></li>
</ul>
</div>
<div class="employeeul">
<ul> <!-- Mitarbeiter -->
<li class="employee"></li>
<li class="employee"></li>
<li class="employee"></li>
<li class="employee"></li>
</ul>
</div>
<div class="employeeul">
<ul> <!-- Mieter -->
<li class="employee"></li>
<li class="employee"></li>
<li class="employee"></li>
</ul>
</div>
</div>
似乎您只需要在页面具有特定url时动态显示/隐藏项目的语法。在这种情况下,这里有一个简单的JS解决方案:
//get an array of elements with the class we're interested in working with
var employeeuls = document.getElementsByClassName("employeeul");
//get the current url
var url = window.location.href;
//if the current url is equal to example.php#profile, hide some elements
if(url == "example.php#profile")
{
//iterate over the array and apply the style to hide the elements
for(i=0; i < employeeuls.length; i++)
{
employeeuls[i].style.display = "none";
}
}
//otherwise, the elements should be hidden
else
{
//iterate over the array and apply the style to hide the elements
for(i=0; i < employeeuls.length; i++)
{
employeeuls[i].style.display = "block";
}
}
如果目标是指URL中的哈希值,则只需编写一些JS来获取该哈希值并切换css。然后通过jQuery切换显示/隐藏或可见性类
$(document).ready(function(){
var $profiles = $('.profile'); // Store all the profiles in a query
var hashTarget = location.hash.replace('#', ''); // Returns hash value
function showTargetedDiv(){
$profiles.hide(); // Hide any divs that may previously be showing
$('#' + hashTarget).show();
}
showTargetedDiv();
$(window).on('hashchange', showTargetedDiv); // Event handler
});
你说有针对性是什么意思?当您使用class='profile'将鼠标移到顶部div上时,是否希望显示下面的列表?您没有制作任何锚定!请结帐。我指的是目标,如果你有地址示例。phpm_mitarbeiter0。如果用户以其中一个ID为目标,我希望所有员工都被隐藏。你如何隐藏你的div?如果您使用CSS并将display属性设置为none,那么您所要做的就是在需要显示div时将display属性更改为block for div。如果您喜欢使用jQuery,并且愿意为您这样做。如果你对jQuery不满意,并且这条评论针对的是你的问题,请告诉我,我会用一个js等价物来写一个答案。基本上这就是我想要的!现在我正在与事件侦听器进行斗争。如果url为example.phpprofile*则应触发该事件好的,我将更新我的答案以反映这一点。嘿,帮我个忙,接受这个作为你问题的答案?
$(document).ready(function(){
var $profiles = $('.profile'); // Store all the profiles in a query
var hashTarget = location.hash.replace('#', ''); // Returns hash value
function showTargetedDiv(){
$profiles.hide(); // Hide any divs that may previously be showing
$('#' + hashTarget).show();
}
showTargetedDiv();
$(window).on('hashchange', showTargetedDiv); // Event handler
});