English 中文(简体)
如何在添加元素后刷新div的大小
原标题:How to refresh the size of a div after adding elements to it

我有这样一个元素:

<div id="content" style="border: 2px solid black">
</div>

通过JQuery,我添加了一些新内容:

$("#content").append($( <div></div> ).addClass("box"));

哪里

.box { margin: 10px; padding: 10px; border: 1px solid black }

但是当我这样做时,外部div不会调整大小,所以我得到了一个从实心外部框中伸出的内部框。添加此内部框时,如何使外部框调整自身大小?

最佳回答

如果选择器不是打字错误,请更正它

$("#content") // id="content" (correct)
$("content")  // tagName = content

并将div的显示更改为内联块

#content {
    display: inline-block;
}

那么div将不会占据整行。

问题回答

试试这个:

js公司

$( #content ).append( <div class="box">&nbsp;</div> );

html格式

<div id="content" style="border:2px solid black;overflow:hidden;">
</div>

我希望他的帮助!

#content没有调整大小,因为它的width可能是在CSS中设置的。

要么把它做得更宽,要么专门给它适当的尺寸。

看起来#content的宽度必须超过40px,因为内部div有10个边距和10个填充,即左边20个,右边20个。

所以,类似于:

$("#content").css("width","100%").append($( <div></div> ).addClass("box"));

或者更好的是,将CSS设置为正确的宽度,开始时:

#content { width: ... ; }

如果您在#content内浮动.box,则将#content溢出设置为auto(或隐藏),否则外部框不会“看到”浮动内部框:

#content { overflow: auto; }

就我而言,我补充道:

#property { overflow: auto; }

现在,当我显示或隐藏元素时,元素的大小正在重新计算。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.