javascript (jquery) 添加文本注释不要换行/忽略它们的 div 容器

标签 javascript css html overlap line-breaks

我正在开发基于 JavaScript 的评论系统:

这是生成评论 html 的 Javascript:

    $.each(comments.user,function(key,value)
        {
        comment_string += '<div class = "r_comment_header">';
        comment_string+= '<a class = "r_comment_user" href = "profile.php?id=' + comments.user_id[key] + '" title = "Profile of ' + value + '">' + value + ' </a>';
        if(comments.pm_button[key])
            comment_string+= '<input type = "button" class = "comment_send_pm" value = "Message" name = "' + comments.user_id[key] + '" title = "Send this user a private message"/>';
        comment_string+= '<span class = "r_comment_time">' + comments.time[key] + '</span>';
        comment_string+= '</div>';
        comment_string+= comments.content[key];
        comment_string+= '<div class = "comment_abuse_wrapper">';
        comment_string+= '<input type = "button" class = "comment_report_abuse" name = "' + comments.id[key] + '" value = "Report abuse" title  = "Report this comment as inappropriate content"/>';
        comment_string+= '</div>';
        });

    $('#request_comments').html(comment_string);

现在,当我通过文本输入字段添加新评论时,评论的内容会忽略 div 容器边界并且不会换行:

http://i.imgur.com/V85Vc.png

这是 div 容器的 css:

#request_comments
{
width:658px;
padding: 10px;
margin: 10px 0 0 10px;
}

有什么建议吗?

最佳答案

这是正常的 HTML 行为。如果您有一个不可包装的字符串(即没有符号/空格)并且与 JavaScript 或 jQuery 完全无关,就会发生这种情况。

最简单的解决方案是使用 overflow: hidden CSS 选项。

PS:您应该考虑使用模板,而不是仅仅通过字符串创建标记。现在有一些不错的 JavaScript 模板引擎。看看http://api.jquery.com/jquery.tmpl/例如。

关于javascript (jquery) 添加文本注释不要换行/忽略它们的 div 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7260651/

相关文章:

javascript - 带有简单数组的基本 ng-repeat

javascript - 如何计算 2 个 Javascript 对象之间的匹配百分比?

javascript - 从父级下拉菜单中 react 设置子级状态

css - IE8和Firefox CSS选择器的区别?

html - 居中固定/锚定页脚?

jQuery 比例尺去除边框

javascript - Ember.js 2. 使用嵌套模板会消耗更多 RAM 吗?

java - org.springframework.web.servlet.PageNotFound - 未找到具有 URI 的 HTTP 请求的映射

html - 如何让 div、header、container 等覆盖视口(viewport)

html - 将标题颜色线设置为循环 CSS3