简体   繁体   English

换行时通过JavaScript更改DIV的高度

[英]Change DIV's height via JavaScript when word-wrap occurs

I don't even know whether this is possible, however, I am hoping that JavaScript could provide a solution to this problem. 我什至不知道这是否可行,但是,我希望JavaScript可以为这个问题提供解决方案。 I have a DIV, that shows the title for each page within a WordPress template that I am working on. 我有一个DIV,它显示了我正在处理的WordPress模板中每个页面的标题。

<div class="grid-block" id="page">

  <div id="page-info">

    <h3>#<?php is_home() ? bloginfo('description') : wp_title("",true); ?></h3>

  </div><!-- #page-info -->

</div><!-- .grid-block #page -->

The text that is called into the DIV comes in at various lengths, and sometimes over-exceeds the DIV. DIV中调用的文本有各种长度,有时超过DIV。 When the text exceeds the DIV, it wraps, just as it should, however, I am attempting to adjust the 'height' of this DIV, after a word is wrapped. 当文本超出DIV时,它会像应该的那样自动换行,但是,我尝试在换行后调整该DIV的“高度”。 I do not know if I could add some type of eventListener or something, however, pure HTML and/or CSS does not seem to have the components I need to solve this problem. 我不知道是否可以添加某种类型的eventListener或其他内容,但是,纯HTML和/或CSS似乎没有解决此问题所需的组件。

In addition, I understand that I 'could' use @media (media queries) to sort of emulate this effect, however, as far as I know, this can only be done in relation to the width of the Window, and I want the DIV to re-size 'only' when a string exceeds the width of this DIV. 另外,我知道我可以“使用” @media(媒体查询)来模拟这种效果,但是据我所知,这只能与Window的宽度有关,我希望当字符串超出此DIV的宽度时,DIV会“仅”调整大小。 A demo of what I am attempting to do can be found at http://jsfiddle.net/justinbyrne001/SP3Q2/4/ . 我正在尝试做的演示可以在http://jsfiddle.net/justinbyrne001/SP3Q2/4/找到。 I appreciate any comments, recommendations, and advice that anyone has regarding this matter. 感谢任何人对此事的任何评论,建议和意见。 Thanks in advance. 提前致谢。

Do you need the fix height on #page-info? 您是否需要#page-info上的固定高度? If you just use padding:10, the div#page-info would automatically wrap around the text. 如果仅使用padding:10,则div#page-info会自动在文本周围换行。

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

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