admin管理员组文章数量:1426847
How do I set the width and height of a hidden element by using another hidden element's width and height?
for(i=1;i<10;i++){
$('#hiddenE1'+i).width($('#hiddenE2'+i).width()).height($('#hiddenE2'+i).height());
}
How do I set the width and height of a hidden element by using another hidden element's width and height?
for(i=1;i<10;i++){
$('#hiddenE1'+i).width($('#hiddenE2'+i).width()).height($('#hiddenE2'+i).height());
}
Share
Improve this question
asked Apr 11, 2015 at 19:17
FergosoFergoso
1,5823 gold badges22 silver badges45 bronze badges
3
- 1 Your code is correct, assuming you got the selectors right. – Ninjakannon Commented Apr 11, 2015 at 19:23
-
@Ninjakannon: Thanks. yes, the code will work only if the elements are not hidden. But the code doesn't work when all
$('#hiddenE1'+i)
and$('#hiddenE2'+i)
are hidden. – Fergoso Commented Apr 11, 2015 at 19:27 -
2
@Ninjakannon is right. Maybe one thing you're tripping over is that it doesn't effect the DOM since they're hidden. Are you using
display: none
to hide them, tryvisibility: hidden
instead and you should see the width/height changes – ACruz Commented Apr 11, 2015 at 19:29
2 Answers
Reset to default 6There is no width when display: none
. One trick would be to set position: relative; margin-left: -9999px;
. It will not show on the page but preserve the width. Or, use visibility: hidden
. The only downside is it will not rip it out of the layout but leave a blank spot.
One can create a class with styles to hide the element i.e.
.hide {
position: absolute;
top: -9999px;
left: -9999px;
}
In case you'd also want to revert you can use another class e.g.
.unhide {
position: initial;
top: initial;
left: initial;
}
initial is for the default value, otherwise one could use known values read about initial
There are however other ways to hide elements as mentioned on this post
In case the link doesn't work:
1.
.hide {
opacity: 0;
}
2.
.hide {
visibility: hidden;
}
3.
.hide {
display: none;
}
4.
.hide {
clip-path: polygon(0px 0px,0px 0px,0px 0px,0px 0px);
}
本文标签: javascriptSet the width and height of a hidden elementStack Overflow
版权声明:本文标题:javascript - Set the width and height of a hidden element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483941a2660298.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论