javascript Jquery 创建元素最初是隐藏的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5255593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery create element initially hidden
提问by gdanko
Is there a way to create an element and have it hidden initially? I am creating an iframe but don't want it shown right away.
有没有办法创建一个元素并在最初隐藏它?我正在创建一个 iframe,但不希望它立即显示。
回答by gilly3
Very simple. Just do this:
很简单的。只需这样做:
var myFrame = $("<iframe>").hide();
回答by Kyle
var my_iframe = $('<iframe name="your_iframe" src="your_source"></iframe>');
now my_iframeholds your jQuery created iframe. Modify it, do what you wish and then put it in the dom.
现在my_iframe保存您的 jQuery 创建的 iframe。修改它,做你想做的,然后把它放在dom中。
It wont be visible until you insert it into the dom.
在您将其插入 dom 之前,它是不可见的。
回答by Pointy
Just create it and style it as being hidden, in one of many possible ways, like this:
只需以多种可能的方式之一创建它并将其设置为隐藏的样式,如下所示:
var secretThing = $('<iframe></iframe>', { css: { 'display': 'none' }});
$('body').append(secretThing);
Another way to make something hidden is to position it far off the viewport, or to put it behind something else, or to set some dimension to zero. It depends on the rest of your design. Personally, I'd be inclined to give the element a class value that makes it hidden.
隐藏某些东西的另一种方法是将其放置在远离视口的位置,或者将其放在其他东西的后面,或者将某个维度设置为零。这取决于您设计的其余部分。就个人而言,我倾向于给元素一个类值,使其隐藏。
(@gilly3 wisely notes that the handy jQuery "hide" function might be a simple way to do this.)
(@gilly3 明智地指出,方便的 jQuery“隐藏”功能可能是一种简单的方法。)
回答by Dre
var theElement = <create the iframe here>;
theElement.hide();
// append theElement here
回答by Dre
Like this:
像这样:
var FName = $("<iframe>").hide();

