0

我写了一段代码

$(".ka_opinions_quote a").click(function(){
    var quoted_text = $(this).parents("div.o_opinion").find(".ka_opinion_txt").text();
    var cite_uri = $(this).parents("div.o_opinion").prev().find("a").attr("href");
    var curr_text = $(".editor").text();

    $(".editor").text(curr_text+'<quote cite="<?php echo mb_substr(JURI::base(), 0, -1); ?>'+cite_uri+'">'+quoted_text+'</quote>');
});

单击文本后插入文本区域。但是,如果用户在 textarea 中插入或写入一些文本或删除文本并单击引用链接后,文本不会显示在 textarea 但在 Firebug 中我已经看到但实际上文本已经存在

替代文字

任何想法如何实现正确的文本插入?

4

2 回答 2

1

我已经修改了 markitup 附带的示例(查看Markitup 中的index.html!存档),这里我们得到了什么:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="markitup/jquery.markitup.js"></script>
<script type="text/javascript" src="markitup/sets/default/set.js"></script>
<link rel="stylesheet" type="text/css" href="markitup/skins/markitup/style.css" />
<link rel="stylesheet" type="text/css" href="markitup/sets/default/style.css" />
</head>
<body>
<script type="text/javascript">
$(document).ready(function()    {
    $('#markItUp').markItUp(mySettings);
    $('.add').click(function() {
        $.markItUp( {   openWith:'<opening tag>',
                        closeWith:'<\/closing tag>',
                        placeHolder:document.getSelection()
                    }
                );
        return false;
    });
});
</script>
<p>Click <a href="#" class="add">this link to insert content</a> from anywhere in the page</p>
<p>
<textarea id="markItUp" cols="80" rows="20">
&lt;h1&gt;Welcome on markItUp!&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;markItUp!&lt;/strong&gt; is a JavaScript plugin built on the jQuery library. It allows you to turn any textarea into a markup editor. Html, Textile, Wiki Syntax, Markdown, BBcode or even your own markup system can be easily implemented.&lt;/p&gt;
</textarea>
</p>
</body>
</html>

您在文本区域中设置光标位置,选择文本并单击链接“...添加内容...”。脚本只是将您选择的文本放入光标位置的 textarea 中。很抱歉使用document.getSelection() - 在 jQuery 中没有找到任何类似的实用函数 =)

于 2010-12-24T14:21:50.117 回答
0

这取决于您使用的编辑器。对于markItUp,您可以使用这些;

$.markItUp({ name:"YourWord", openWith:"[b]", closeWith:"[/b]" })
$.markItUp({ replaceWith: "yourCustomEntry" });
于 2010-12-24T16:27:39.580 回答