Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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/2/jquery/74.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 - Fatal编程技术网

执行并动态查看javascript函数的结果

执行并动态查看javascript函数的结果,javascript,jquery,Javascript,Jquery,我正在制作一个简单的网络应用程序。在一个特定的页面中,我必须动态生成一个列表,并在列表中的每个项目之后都有一个日历 代码如下: for (var i = 0; i < goalsObj.length; i++) { var node = document.createElement("li"); node.setAttribute("draggable", "true"); node.setAttribute('id', 'g' + i); node.add

我正在制作一个简单的网络应用程序。在一个特定的页面中,我必须动态生成一个列表,并在列表中的每个项目之后都有一个日历

代码如下:

for (var i = 0; i < goalsObj.length; i++) {
    var node = document.createElement("li");
    node.setAttribute("draggable", "true");
    node.setAttribute('id', 'g' + i);
    node.addEventListener('click', function () {
        viewGoal()
    }, false);
    var textnode = document.createTextNode(goalsObj[i]);
    node.appendChild(textnode);
    var cal = "<script>calendar()</script>";
    var calen = document.createTextNode(cal);
    document.getElementById("sortable").appendChild(node);
    document.getElementById("sortable").appendChild(calen);
}
for(变量i=0;i

但问题是,不是计算并显示
calendar()
函数的输出,而是到处都是
“script calendar()/script”
。发生了什么?我该怎么办?

您不需要为此创建脚本元素。如果我理解正确,那么您只需要显示
calendar()
的结果。因此,您只需调用函数并将结果传递给
createTextNode
函数:

var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.appendChild(document.createTextNode('calendar();'));
var calen = document.createTextNode(calendar());
您现在要做的是创建一个字符串
“calendar()”
,然后创建一个以该字符串为内容的textnode。不会执行任何操作,因为
createTextNode
就是这样做的,它会创建一个TEXTnode。

使用

var calen = document.createElement("div");
document.getElementById("calen").innerHTML=calendar();

... 我真的不明白为什么你看不到答案,或者你不明白
createTEXTnode
的意思……你应该用转义字符作为符号。它不起作用。我将“未定义”作为输出。还有什么是正确的做法?@coder在这种情况下,日历函数可能返回
未定义的
。所以这个函数中肯定还有一个错误。这里有一个提琴演示了它的工作原理:这是最后几行:calendar_html+='';日历_html+='';返回日历和html;日历功能的一部分。我该怎么办?