Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在匿名函数中显示函数输出_Javascript_Jquery_Html_Variables - Fatal编程技术网

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

Javascript 在匿名函数中显示函数输出,javascript,jquery,html,variables,Javascript,Jquery,Html,Variables,我试图在javascript代码中输出所选文本。我创建了一个函数来获取所选文本,我只想在输出submit按钮时输出它 下面是获取所选HTML或文本的javascript function getSelectionHtml() { var html = ""; if (typeof window.getSelection != "undefined") { var sel = window.getSelection(); if (sel.rangeC

我试图在javascript代码中输出所选文本。我创建了一个函数来获取所选文本,我只想在输出submit按钮时输出它

下面是获取所选HTML或文本的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);

}
现在,这一行负责显示所选文本和其他属性。基本上,我创建了一个HTML变量来捕获所选文本,但是它不会显示在我的代码中。查看代码:

onsubmit: function( e ) 
{
    editor.insertContent( '[tooltip class="' + e.data.listboxClassName + '" title="' + e.data.textboxtooltipName + '"]  html  [/tooltip]'); 
}
知道怎么回事吗?如何将getSelectionHtml函数的输出正确显示为匿名函数的输出?

试试看

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!
}

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

尝试:


“基本上,我创建了一个HTML变量来捕获所选文本”-我没有得到这部分。哪里为什么要使用
document.write(html)?是否要用所选文本替换整个文档?您好,TJ我使用html变量在窗口上获取所选文本,然后将其与匿名函数上的其他属性一起输出。如果您是指函数中的以下代码:
editor.insertContent(“[tooltip class=”+e.data.listboxClassName+”)title=“'+e.data.textboxtooltipName+']html[/tooltip]”)
那里
html
是一个字符串,不是变量。即使它是,你也不能在生活中访问它,除非它是全局的。你能帮我修复代码吗?这样我也可以检查你的意思吗?提前谢谢!我仍然没有得到选中/突出显示的html或文本。它根本没有显示任何内容。检查控制台中的错误:如果没有显示任何内容在那里,将
//document.write(html);
替换为
警报(html)
控制台.log(html)
,查看函数是否实际获取文本。逐个检查流程。这称为调试。除非我们有完整的代码或演示,否则我们无法帮助您完成此操作。以上是我看到的有关共享代码的修复。祝你好运。它根本没有获取HtmlTJ@SamNorton那是另一个问题。你应该检查一下在问类似的问题之前,unction是否起作用。你的问题应该是相反的,这当然是重复的。检查该代码的源代码并做一些研究。
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
}
editor.insertContent(
    '[tooltip class="' + e.data.listboxClassName
    + '" title="' + e.data.textboxtooltipName + '"]'
    + editor.selection.getContent()
    + '[/tooltip]');