当 div 的内容发生更改时,如何自动调整 div 的大小以适应其内容的大小?
目前,我有这个 DIV,其中有一个注册表单位于页面中央。 DIV 的内容来自 ascx 页面。这做得很好。 现在,如果用户尝试填写不唯一的名称,某些 jQuery 会在用户名字段旁边添加一条错误消息。这破坏了 DIV 的布局,因为现在的内容比以前更宽。 所以我用谷歌搜索了这个问题,但找不到解决方案。
任何人都可以帮我找到一个很好的方法来做到这一点(伪 HTML/js):
<div id="myCenteredDiv" onChangeContents="resizeToNewSize()">
<!-- div contents -->
</div>
<script type="text/javascript">
function resizeToNewSize() {
$("#myCenteredDiv").animateToSize($("#myCenteredDiv").contentWidth,
$("#myCenteredDiv").contentHeight);
}
</script>
我正在寻找“onChangeContents”方法(和/或事件)和“div.contentWidth”属性。
非常感谢您帮助我!
更新:试图更清楚地解释问题
假设我有这个 DIV:
<div id="myDiv">
<form ...>
Enter your name: <input id="txtYourName" type="text" name="YourName" value="" />
<span id="errorYourName"></span>
<input type="submit" ... />
</form>
</div>
假设我有这个 jQuery 片段:
$(document).ready(function() {
$("#txtYourName").live("blur", function() {
if (validateInput($("#txtYourName").val())) {
$("#errorYourName").html("");
// entry 1
} else {
// entry 2
$("#errorYourName").html("This name is not valid.");
}
});
});
...其中 validateInput(value)
返回 < code>true 为有效值。
现在好了。 SimpleModal 插件获取 div 并将其放置在页面中央,并具有一定的宽度和高度,它以某种方式读取 div 的内容。因此 div 不比输入框宽,因为此时 span 是空的。
当输入框失去焦点时,将在跨度中放入一条错误消息。这会破坏 div 的布局。
我可以将代码放入 entry 1
中,当错误消息被清除时,通过动画将 div 大小调整回某个大小,并将代码放入 entry 2
中,将 div 大小调整为更大的尺寸,以便错误消息能够容纳。
但我最想要的是一种方法来判断 div 中的内容是否已更改,并相应地以动画方式自动适应 div。
有什么想法吗?再次感谢。
At the moment, I have this DIV with a registration form centered over the page. The contents of the DIV come from an ascx-page. This is done nicely.
Now, if the user tries to fill in a name that's not unique, an error message is added by some jQuery next to the username field. This breaks the layout of the DIV, since the contents now are wider than they used to be.
So I've googled my way into this, but I can't find a solution for this.
Can anyone help me find a nice way to do this (pseudo HTML/js):
<div id="myCenteredDiv" onChangeContents="resizeToNewSize()">
<!-- div contents -->
</div>
<script type="text/javascript">
function resizeToNewSize() {
$("#myCenteredDiv").animateToSize($("#myCenteredDiv").contentWidth,
$("#myCenteredDiv").contentHeight);
}
</script>
I'm looking for the "onChangeContents" method (and/or event) and the "div.contentWidth" property.
Thanks a lot for helping me out!
update: trying to explain the problem more clearly
Let's say I have this DIV:
<div id="myDiv">
<form ...>
Enter your name: <input id="txtYourName" type="text" name="YourName" value="" />
<span id="errorYourName"></span>
<input type="submit" ... />
</form>
</div>
And let's say I have this snippet of jQuery:
$(document).ready(function() {
$("#txtYourName").live("blur", function() {
if (validateInput($("#txtYourName").val())) {
$("#errorYourName").html("");
// entry 1
} else {
// entry 2
$("#errorYourName").html("This name is not valid.");
}
});
});
...where validateInput(value)
returns true
for a valid value.
Well now. The SimpleModal plugin takes the div and places it centered on the page, with a certain width and height it somehow reads off of the contents of the div. So the div isn't wider than the input box, since the span is empty at the moment.
When the input box loses focus, an error message is put into the span. This then breaks the layout of the div.
I could put code into entry 1
that resizes the div back with an animation to some size when the error message has been cleared, and code into entry 2
that resizes the div to a bigger size so the error message will fit.
But what I'd like best is a way to tell if the contents within the div have changed, and fit the div accordingly, animated and automatically.
Any ideas? Thanks again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery:
div:
预览
http://jbdes.net/dev/js/fontsize.html
Jquery:
div:
preview
http://jbdes.net/dev/js/fontsize.html
我从未使用过 SimpleModal,但从他们网站上的示例来看,您似乎可以设置容器 CSS。如果您希望调整高度,请尝试将高度设置为
auto
虽然示例中没有,但我认为您需要在高度后面添加
px
并引号中的宽度(例如"450px"
)。好的,这是另一个想法。也许这太多了,但是添加一个隐藏的输入字段:
然后附加一个在更新错误消息的同时触发的更改事件。
这未经测试,可能有点过分,但我在 SimpleModal 中没有看到更新功能。
更新:抱歉,我发现现场活动不支持模糊。所以我做了一些进一步的测试并提出了一个可行的演示。我将其发布在此pastebin中(忽略底部包含的simpleModal代码)。 基本代码
这是CSS
HTML
脚本的
I've never used SimpleModal, but from the examples on their site it looks like you can set the container CSS. If you want the height to adjust, try setting the height to
auto
Although the example doesn't have it, I'd think you need to add
px
after the height and width in quotes (e.g."450px"
).Ok here is another idea. Maybe this is too much, but add a hidden input field:
then attach a change event which is triggered at the same time you update the error message.
This is untested and it may be going overboard, but I don't see an update function in SimpleModal.
Update : Sorry I figured out that blur isn't supported with live event. So I did some further testing and came up with a working demo. I posted it in this pastebin (ignore the included simpleModal code at the bottom). Here is the essential code
CSS
HTML
Script
老式表格(仍然)非常擅长将事物居中,快速、流畅且无需代码。
Old fashioned tables are (still) great at centering things, fast, fluid and code free.