在匿名函数中显示函数输出

时间:2014-08-18 02:26:52

标签: javascript jquery html variables

我正在尝试在我的javascript代码中输出SELECTED TEXT。我创建了一个函数来获取所选文本,我只想在输出提交按钮时输出它。

以下是获取所选HTML或TEXT的JavaScript。

function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
   document.write(html);

}

这是我输出其他内容和上述功能的代码(但它不会出现)

(function() 
{
    tinymce.PluginManager.add('my_mce_button', function( editor, url ) 
    {
        editor.addButton( 'my_mce_button', 
        {
            icon: false,
            text: 'Tooltip',
            onclick: function() 
            {
                editor.windowManager.open( 
                {
                    title: 'Tooltip Option',
                    body: 
                    [
                        {
                            type: 'textbox',
                            name: 'textboxtooltipName',
                            label: 'Tooltip Text',
                            value: ''
                        },
                        {
                            type: 'listbox',
                            name: 'listboxClassName',
                            label: 'Class',
                            'values': 
                            [
                                {text: 'Top Tooltip', value: 'top_tooltip'},
                                {text: 'Left Tooltip', value: 'left_tooltip'},
                                {text: 'Right Tooltip', value: 'right_tooltip'},
                                {text: 'Bottom Tooltip', value: 'bottom_tooltip'}
                            ]
                        }
                    ],
                    onsubmit: function( e ) 
                    {
                        editor.insertContent( '[tooltip class="' + e.data.listboxClassName + '" title="' + e.data.textboxtooltipName + '"]  html  [/tooltip]');
                    }
                });
            }
        });
    });
})();

现在这一行是负责显示SELECTED TEXT +其他属性的人。基本上,我创建了一个HTML变量来捕获所选文本,但它没有显示在我的代码上。看看代码:

onsubmit: function( e ) 
{
    editor.insertContent( '[tooltip class="' + e.data.listboxClassName + '" title="' + e.data.textboxtooltipName + '"]  html  [/tooltip]'); 
}

知道怎么了?如何正确地将getSelectionHtml函数的输出显示给匿名函数的输出?

2 个答案:

答案 0 :(得分:0)

尝试

function getSelectionHtml() {
  html = ""; // removed var so that it can be accessed globally.
  if (typeof window.getSelection != "undefined") {
    var sel = window.getSelection();
    if (sel.rangeCount) {
        var container = document.createElement("div");
        for (var i = 0, len = sel.rangeCount; i < len; ++i) {
            container.appendChild(sel.getRangeAt(i).cloneContents());
        }
        html = container.innerHTML;
    }
  } else if (typeof document.selection != "undefined") {
    if (document.selection.type == "Text") {
        html = document.selection.createRange().htmlText;
    }
  }
 //document.write(html); this will rewrite the document - don't do this!
}

并在匿名函数中:

onsubmit: function( e ) 
{
   getSelectionHtml(); // get selected the text
   if(html) // check whether text is selected
       editor.insertContent( '[tooltip class="' + e.data.listboxClassName + '" title="' + e.data.textboxtooltipName + '"]'+  html+'  [/tooltip]'); // html now reffers to the global variable
}

答案 1 :(得分:0)

您的getSelectionHtml()函数会在页面上选择文本,但我认为您希望在tinymce编辑器中选择文本。

尝试:

editor.insertContent(
    '[tooltip class="' + e.data.listboxClassName
    + '" title="' + e.data.textboxtooltipName + '"]'
    + editor.selection.getContent()
    + '[/tooltip]');
相关问题