jquery - facebook 喜欢标签 - word-wrap :break-word; doesnt work on FF and IE9+

标签 jquery css facebook

我正在尝试插入 <b>每当 keyup , 一个 #被检测到(如 Facebook )。我从 textarea 读到, 并复制到 div元素。

    <div  class="new_postAdDescription2" id="new_postAdDescription2" spellcheck='false' contenteditable='true'> </div>  
    <textarea name="description" id="new_postAdDescription" spellcheck='false' 
class="new_postAdDescription" cols="30" rows="10" placeholder="Posto nj&euml; shpallje" >
</textarea>

请看我如何替换空格和新行。我试过&nbsp;以及(顺便说一下,如果我删除空格匹配,一切正常!)

$('#new_postAdDescription').keyup(function (e) {
        var str = $('#new_postAdDescription').val();

        str = str.replace(/ /g, '&#160;');
        str = str.replace(/\n/g, '<br/> ');

        str = str.replace(/(&#160;|<br\/> )#([a-zA-Z0-9]+)/g, "$1<b class='highlighterContent'>#$2</b>");

        $('#new_postAdDescription2').html(str);  
    });

//样式

<style>
.new_postAdDescription, .new_postAdDescription2{
    position: relative;
    float: left;
    background-color: transparent;
    border: 0;
        -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box;         /* Opera/IE 8+ */
    outline: 0;
    width: 694px;
    color:#1c1c1c;
    resize: none;
    margin: 10px;
    height: 115px;
    font-size:13px;
    line-height:1.3;
    direction: ltr; 
    word-wrap:break-word;
}

.new_postAdDescription2{
position:absolute;
color:transparent;
word-wrap:break-word;
direction: ltr; 
text-align: left;
}
.highlighterContent{
position:relative;
font-weight:bold;
color:#333;
background-color:#f2f2f2;
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box;         /* Opera/IE 8+ */
}

</style>

它不是最好的,但它在 Chrome 上运行良好,但在 FF 和 IE9 上,当涉及到断词时,它会与文本重叠并丢失轨道。看屏幕截图

FFIE9

enter image description here

Chrome 中:

enter image description here

最佳答案

我确实找到了正确答案....

新的 javascript 是(简单得多):

$('#new_postAdDescription').keyup(function (e) {
    var str =$('#new_postAdDescription').val();

    str = str.replace(/\n/g, '<br>');
    str = str.replace(/#([a-zA-Z0-9]+)/g, "<b>#$1</b>");

    $('#new_postAdDescription2').html(str);
});

和在 css 中:

.new_postAdDescription, .new_postAdDescription2{
    position: relative;
    float: left;
    background-color: transparent;
    border: 0;
    outline: 0;
    width: 694px;
    resize: none;
    margin: 10px;
    height: 115px;
    font-size:13px;
    line-height:1.3;
    direction: ltr;
    color:#1c1c1c;
}

.new_postAdDescription2{
    position:absolute;
    color:transparent;
    white-space: pre-wrap;
}
b{
font-weight:bold;
color:#333;
display: inline-block;
white-space: pre-wrap;
word-wrap:break-word;
direction: ltr; 
text-align: left;
max-width: 100%;
}

添加空白:预包装;在正确的地方让它发挥作用。

关于jquery - facebook 喜欢标签 - word-wrap :break-word; doesnt work on FF and IE9+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17215976/

相关文章:

javascript - 为什么本地存储无法工作,即使它在页面刷新后不应打开?

javascript - 显示更多/显示更少 Jquery 代码可以在 fiddle 上运行,但不能在我的代码中运行

javascript - 如何在加载页面的所有其他内容后加载php页面

html - 在 hugo 主题中,我想用 css 更改 Markdown 单反引号高亮颜色

php - 用户通过Facebook登录后,我应该如何处理用户数据

javascript - 在 php 中从网站 Facebook 共享

javascript - jQuery.get() 失败并返回完整的 url

javascript - 如何根据javascript变量更改html标题innertext

html - 为什么 CSS mask-image 不能使用 no-repeat?

iphone - 如何查找安装在 iPhone 中的应用程序