div display没有破坏风格

时间:2014-11-16 22:51:37

标签: javascript html css

我有一个textarea用于评论和一个按钮来显示或隐藏它(切换)。如果我想默认隐藏它(显示:无)当我单击按钮显示它时,样式被破坏但如果它没有被隐藏(显示:块)我可以没有问题地点击,样式就可以了。

HTML:

<a id="1" class="comment_button a_button" title="Dejar un comentario">Comentar</a>

<div id="hide_1" class="rows" style="display: none;">
    <ul id="UL_101">
        <!-- New comment row -->
        <li class="newComment_row">
            <div class="userComments_photo">
                <img class="photo" src="/images/profile/' . $_SESSION['photo'] .'" alt="" />
            </div>
            <textarea id="' . $imgID . '" class="textarea" rows="1" placeholder="Escribe un comentario..." title="Escribe un comentario..."></textarea>
        </li>
    </ul>
</div>

的CSS:

.rows {
    height: auto;
    width: 494px;
    background: rgb(246, 247, 248) none repeat scroll 0% 0% / auto padding-box border-box;
    border-radius: 0 0 3px 3px;
    margin: 8px -12px -12px;
}
#UL_101 {
    width: 494px;
    list-style: none outside none;
    margin: 0px;
    padding: 0px;
    border-top: 1px solid rgb(225, 226, 227);
}
/* li */
 .newComment_row {
    height: auto;
    position: relative;
    width: 470px;
    background: rgb(246, 247, 248) none repeat scroll 0% 0% / auto padding-box border-box;
    border-radius: 0 0 2px 2px;
    list-style: none outside none;
    margin: 0px 12px;
    padding: 4px 0px 8px 0px;
}
.textarea {
    resize: none;
    width: 416px;
    font: normal normal normal 12px/15.3599996566772px Helvetica, Arial, 'lucida grande', tahoma, verdana, arial, sans-serif;
    position: initial;
    padding: 8px 3px 0 3px;
    margin: 0 7px;
    overflow: hidden;
}

和jquery:

// show rows
$('.comment_button').on('click', function () {
    $('#hide_' + this.id).slideToggle('fast');
});

function h(e) {
    $(e).css({
        'height': 'auto',
        'overflow-y': 'hidden'
    }).height(e.scrollHeight);
}
$('textarea').each(function () {
    h(this);
}).on('input', function () {
    h(this);
});

display:none打破了样式:http://jsfiddle.net/cb41hmpo/
display:block不会破坏它:http://jsfiddle.net/cb41hmpo/1/

似乎问题是自动高度功能......有没有办法解决这个问题?

如果可能的话,我想保留textarea尺寸,无论有什么变化。

这不是什么大问题,但是如果将显示设置为阻止并且我单击按钮,textarea占位符文本会出现一些模糊或模糊一秒,是正常的还是可以修复?

1 个答案:

答案 0 :(得分:1)

如果我们查看您的代码

function h(e) {
  alert(e.scrollHeight);
  $(e).css({
    'height': 'auto',
    'overflow-y': 'hidden'
}).**height(e.scrollHeight)**;
}

如果你看一下这里的粗体部分,你将scrollHeight分配给$ e。如果我们发出警报,我们可以看到隐藏父div时textarea的高度为0,当显示div为23 px时它为23。现在在(星号标记 - &gt; height(e.scrollHeight)文本中,我们明确地将该高度分配给textarea(粗体文本)。因此,它的大小更小。因此, height auto 由于您通过e.scrollHeight分配高度,因此不会进入图片。

尝试从两个代码段中删除粗体文本。你得到的结果是一样的。

希望这会有所帮助。

快乐学习:)

相关问题