javascript - 对象中的 2 个表情符号未正确替换 javascript

标签 javascript jquery

我正在尝试用图像替换表情符号代码。大多数脚本工作正常。但是我在表情符号代码方面遇到了一些问题 >:(<3) .表情码<3)根本不起作用,没有任何东西被替换。还有表情码>:(替换为表情符号代码 :( 的表情符号和 >标志留在前面。

  1. 为什么是<3)没有被替换?

  2. 为什么是>:(替换为 :( 的代码什么时候>:(:(之上 在对象中?

//-------------------------------------------------
// Replace emoticons with images
//-------------------------------------------------

$(document).ready(function() {
    var emoticons = [
        { "code" : /:\)/ , "img" : "smile.svg" },
        { "code" : /;\)/ , "img" : "wink.svg" },
        { "code" : /:\*/ , "img" : "kiss.svg" },
        { "code" : /:\// , "img" : "unsure.svg" },
        { "code" : /:'\(/ , "img" : "cry.svg" },
        { "code" : /:p/ , "img" : "tongue.svg" },
        { "code" : /:D/ , "img" : "grin.svg" },
        { "code" : />:\(/ , "img" : "grumpy.svg" },
        { "code" : /:o/ , "img" : "astonished.svg" },
        { "code" : /:O/ , "img" : "afraid.svg" },
        { "code" : /8-\)/ , "img" : "nerd.svg" },
        { "code" : /8\)/ , "img" : "sunglasses" },
        { "code" : /:@/ , "img" : "angry.svg" },
        { "code" : /:\(/ , "img" : "frowny.svg" },
        { "code" : /<3\)/ , "img" : "love.svg" },
        { "code" : /:s/ , "img" : "confused.svg" },
        { "code" : /-_-/ , "img" : "dejected.svg" },
        { "code" : /\^\^/ , "img" : "laugh.svg" },
        { "code" : /:\|/ , "img" : "big_eyes.svg" },
        { "code" : /:x/  , "img" : "silent.svg" }
    ];

    $("#text-editor__box").on("keyup", function () {
        var element = document.getElementById("text-editor__box");
        var content = $("#text-editor__box")[0].innerHTML;

        for (var i = 0; i < emoticons.length; i++) {
            if(content.match(emoticons[i]["code"])) {
                $("#text-editor__box")[0].innerHTML = content.replace(emoticons[i]["code"], emoticons[i]["img"]);
            }
        }
    });
});
div { border: 1px solid; min-height: 1.2em }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div contenteditable id="text-editor__box"></div>

最佳答案

原因是您不是在阅读纯文本,而是在阅读 HTML(通过 innerHTML ),而在 HTML 中是文字 <编码为 &lt; .同样>&gt; .

解决方案是阅读带有 textContent 的内容属性代替:

    var content = $("#text-editor__box")[0].textContent;

或者为什么不完全使用 jQuery:

    var content = $("#text-editor__box").text();

第二个问题的发生是因为您一直在使用相同的 content值,即使在对 innerHTML 进行了更改之后的元素。因此,只有最后 匹配才能确定对innerHTML 的最终分配。 .

您可以通过首先对 content 进行所有更改来解决此问题字符串,并且只更新 innerHTML循环后:

$("#text-editor__box").on("keyup", function () {
    var content = $("#text-editor__box").text(),
        changed = false;
    for (var i = 0; i < emoticons.length; i++) {
        if(content.match(emoticons[i]["code"])) {
            content = content.replace(emoticons[i]["code"], emoticons[i]["img"]);
            changed = true;
        }
    }
    if (changed) $("#text-editor__box").html(content);
});

关于javascript - 对象中的 2 个表情符号未正确替换 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48961628/

相关文章:

javascript - http调用成功后如何在for循环内调用http调用?

javascript - 如何在画廊 View 中的一系列动态 div 中使用 Jquery 更改最后一个弹出窗口(动态)的位置

javascript - javascript 中的 Jquery .on() native 函数

php - Codeigniter + Ajax(jquery) 测验

javascript - 从富文本字段内容创建列表项

javascript - 如何在 JavaScript 中执行与 php str_replace 等效的操作?

javascript - 定义 D3 插件 sankey.js 和 html 代码之间的源

javascript - 如何在使用append()方法添加的html元素中显示 Controller 的值?

javascript - 从文本创建对象

javascript - 用于删除方法的 Rails 自定义 Twitter Bootstrap 模式,回调问题