如何通过使用Javascript添加引用标签将高亮显示的文本复制到richtext框编辑器?

时间:2016-10-31 16:26:25

标签: javascript jquery html asp.net

我正在使用ASP.net开发一个论坛网站,当用户试图回复已写的帖子时,他可以引用该帖子的特定部分。

我在asp.net论坛上看到了这个引用的东西 https://forums.asp.net

enter image description here

在那里你可以突出显示文本,当你点击引用按钮时,突出显示的文本将显示在下面的tinymce richtext编辑器中。我有相同的实现。

但我不是javascript的专家。到目前为止,我实现了这个

<script>
    function handlePaste(e) {
        var clipboardData, pastedData;

        // Stop data actually being pasted into div
        e.stopPropagation();
        e.preventDefault();

        // Get pasted data via clipboard API
        clipboardData = e.clipboardData || window.clipboardData;
        pastedData = clipboardData.getData('Text');

        // Do whatever with pasteddata
        alert(pastedData);
    }

    document.getElementById('reference-post-content').addEventListener('paste', handlePaste);
</script>

在那里如果突出显示文本并按CTRL + C然后再次按CTRL + V然后它将通过显示突出显示的文本给出弹出消息。但这不是我想要的。 我想要的是,如果用户点击引用btn,文本应该转到下面的richtextbox。它也应该转到像这样的富文本框。

[quote user="ksss"]

message is in a thread in which the user is a THREAD_PARTICIPANT.

I'm afraid I can never state these

[/quote]

那怎么办呢?在这里,我通过使用chrome在asp.net论坛上进行浏览器调试,找到了这个javascript代码。但似乎没有用。

<script>
    $("#quote-action") != null && $("#quote-action").bind("click", function () {
        var n = null, t = $("#quote-createdby").val();
        if (window.getSelection && (window.getSelection() || window.getSelection().extentNode != null) && window.getSelection().rangeCount > 0) {
            var i = window.getSelection(), u = i.getRangeAt(0).cloneContents(), r = document.createElement("div"); r.appendChild(u); n = r.innerHTML; i.removeAllRanges()
        }
        else document.selection && (n = document.selection.createRange().htmlText, document.selection.empty()); (n == null || n == "") && (n = $("#reference-post-content").html());
        $.ajax({ type: "POST", url: "/soa/post/body/removeprettyprinttags", data: { body: n }, success: function (i) { i != null && i.result != "" && (n = i.result); tinyMCE.activeEditor.focus(); tinyMCE.activeEditor.selection.moveToBookmark(tinyMCE.activeEditor.updatedSelectionBookmark); tinyMCE.execCommand("mceInsertContent", !1, '[quote user="' + t + '"]' + n + "[/quote]"); UpdateBookmark(tinyMCE.activeEditor) }, error: function (i, r, u) { isProd || alert("Error: " + r + " " + u); tinyMCE.activeEditor.focus(); tinyMCE.activeEditor.selection.moveToBookmark(tinyMCE.activeEditor.updatedSelectionBookmark); tinyMCE.execCommand("mceInsertContent", !1, '[quote user="' + t + '"]' + n + "[/quote]"); UpdateBookmark(tinyMCE.activeEditor) } })
    });
</script>

0 个答案:

没有答案