jquery - 跨度不包裹在 div 内

标签 jquery html css

我试图通过在我的字段后面放置一个 div 并复制值来突出显示文本区域中的单词。

$("div").html($("textarea").text()
   .replace("bad-word", "<span style='background-color: red'>bad-word</span>"))

我模拟了它,它在 Chrome 中运行良好。但在 IE 中,span 似乎不会跳转到 div 的下一行,因为该行中的单词太大而无法容纳。相反,它似乎会强制产生一种断词效果——textarea 不会这样做,因此单词不再对齐。在 IE8 + 9 中测试。

IE 9 screenshot 屏幕截图,IE 9 中的代码结果

这是一个 fiddle .尝试在文本区域的开头添加一个字母,您就会明白我的意思。

最佳答案

LIVE DEMO

$("#g1").val("This is me talking about pizza, cheese and also other pizza too hello! how are you?");

$("#g1").keyup(function(e) {
    keyUp(this);
})

function keyUp( el ) {
    var txtArea = $(el);
    var txtAVal = txtArea .val();
    txtAVal = txtAVal.replace(/pizza/gi, "<span style='background:red'>pizza</span>");
    $("#g2").html(txtAVal );
}

关于jquery - 跨度不包裹在 div 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14082962/

相关文章:

javascript - 如何通过 js 或 jquery 设置子宽度等于父宽度减去 x px?

jquery - npm install --保存 jquery

jquery - 将水平宽度大于窗口宽度的 div 居中

html - 如何使背景图像停留在底部并覆盖整个高度

html - 如何覆盖 Bootstrap CSS 样式?

html - 在一定高度后更改元素的宽度

javascript - 如何使用 jquery isotope 按日期排序(新)

PHP:未显示变量值

html - 列表项背景 anchor 链接

jquery - 如何让页面在 Chrome 和 IE 中显示相同?