在jquery.wysiwyg富文本编辑器中键入“@”时,实现jquery UI自动完成功能以显示建议

时间:2011-05-12 05:33:37

标签: jquery-ui text autocomplete editor

我有link的类似要求,就像我在jquery.wysiwyg富文本编辑器中的@中用户类型时必须自动提示一样,上面的代码链接代码适用于输入类型文本和textarea但不适用于富文本编辑器。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery UI Autocomplete - Default functionality</title>
    <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
    <script src="../../jquery-1.5.1.js"></script>
    <script src="../../ui/jquery.ui.core.js"></script>
    <script src="../../ui/jquery.ui.widget.js"></script>
    <script src="../../ui/jquery.ui.position.js"></script>
    <script src="../../ui/jquery.ui.autocomplete.js"></script>
    <script src="jwysiwyg/jquery.wysiwyg.js"></script>
    <link rel="stylesheet" href="../demos.css">
       <link rel="stylesheet" href="jwysiwyg/jquery.wysiwyg.css" type="text/css" />
    <script>


    $(function() {
$('#tags').wysiwyg({
autoGrow: true,
maxHeight: 250,
focusEditor: true
});
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
function split(val) {
    return val.split(/@\s*/);
}

function extractLast(term) {
    return split(term).pop();
}

$("#tags")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function(event) {
    if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
        event.preventDefault();
    }
}).autocomplete({
    minLength: 0,
    source: function(request, response) {
        var term = request.term,
            results = [];

        /* If the user typed an "@": */
        if (term.indexOf("@") >= 0) {
            term = extractLast(request.term);
            /* If they've typed anything after the "@": */
            if (term.length > 0) {
                results = $.ui.autocomplete.filter(
                availableTags, term);
            /* Otherwise, tell them to start typing! */
            } else {
                results = ['Start typing...'];
            }
        }
        /* Call the callback with the results: */
        response(results);
    },
    focus: function() {
        // prevent value inserted on focus
        return false;
    },
    select: function(event, ui) {
        var terms = split(this.value);
        // remove the current input
        terms.pop();
        // add the selected item
        terms.push(ui.item.value);
        // add placeholder to get the comma-and-space at the end
        terms.push("");
        this.value = terms.join("");
        return false;
    }
});

    });
    </script>
</head>
<body>

<div class="demo">

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <textarea id="tags" ></textarea>
</div>

</div><!-- End demo -->



<div class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source-option.</p>
</div><!-- End demo-description -->

</body>
<script>


</script>
</html>

所以,如果我删除

$('#tags').wysiwyg({
autoGrow: true,
maxHeight: 250,
focusEditor: true
});

这个代码在textarea中工作但不在富文本编辑器中。所以请指导我,以便我可以在富文本编辑器中实现相同的功能。

感谢。

0 个答案:

没有答案