简体   繁体   English

避免隐藏的元素占用空间/无法使visible:true最初显示为:css样式中没有

[英]avoid hidden element occupying space/not possible to make visibility:true which is initially made display:none in css style

<style type="text/css">
#second{
 display:none;
}
</style>

on mouse event i want to pass id's of div which is not related to current element from where i am calling the function. 在鼠标事件中,我想传递与我从中调用函数的当前元素无关的div的ID。

<div id="first">
     ....
     ....
</div>
<div id="second">
     ....
     ....
</div>

<input type="image" src="..." id="notrelated"
 onClick="f('first','second')"
 />

javascript javascript

<script type="text/javascript" language="javascript">
function f(first,second){
document.getElementById(first).style.display='none';
document.getElementById(second).style.visibility='visible';
}
</script>

above function thus helps in hiding and make the div visible which is based on mouse event on image. 因此,上述功能有助于隐藏和使div可见(基于图像上的鼠标事件)。 . . I followed these above steps,after making visibility 'visible' it wont show coz i've made div second display none & i tried making the following changes which results in second div occupying space which i don't prefer 我按照上述步骤进行操作,在将可见性设置为“可见”后,它不会显示因为我没有使div处于第二显示状态,并且我尝试进行以下更改,导致第二个div占用了我不喜欢的空间

<style type="text/css">
#second{
visibility:hidden;
}
</style>

If I understand your question right , you want to pass the ids of other DOM elemnts , this case divs. 如果我正确理解您的问题,则您希望传递其他DOM元素的ID(本例为divs)。 To select other DIVS you will need to add class to those DIVS. 要选择其他DIVS,您将需要向这些DIVS添加类。

<div class = 'someclass1' id ='one'></div>

and on your onclick event pass 并在您的onclick事件传递

$('.someclass1').attr('id');

If I got your question, you just want to pass your div ids in the function. 如果我有您的问题,您只想在函数中传递div ID。 So you can easily pass it in any function. 因此,您可以轻松地在任何函数中传递它。

Like 喜欢

<input type="image" src="..." id="notrelated" onClick="Toggle('first','second')" />

and your js function will work 和您的js功能将工作

function Toggle(first,second){
   document.getElementById(first).style.visibility='hidden'
   document.getElementById(second).style.visibility='visible'
}

But If you want that only passing these id you want to toggle these, then you should change your function to like 但是,如果您只想传递这些ID,则想切换这些ID,则应将函数更改为

function Toggle(first,second){
   var firstDiv = document.getElementById(first);
   var secondDiv = document.getElementById(second);
   firstDiv.style.visibility= firstDiv.style.visibility == 'hidden'?'visible':'hidden';
   secondDiv.style.visibility= firstDiv.style.visibility == 'hidden'?'visible':'hidden';
}

You can check this DEMO 您可以检查此演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM