我试图将动态脚本添加到页面中,但它只是在DOM中显示为txt
I'm trying to add dynamic scripts into a page but its just showing as txt in DOM
我使用JS遍历XML来为单页应用程序在DOM上创建元素,现在我想使用jQuery处理面板进度的逻辑。这是Javascript;
/* XML Script for Screen Output */
var xmldata = `<?xml version="1.0" encoding="utf-8" ?>
<script classes="" title="Example Script" brand1="" script1="">
<agent brand="Genius PPT" name="Test User" status="Demonstration">
</agent>
<panels number="8">
<panel classes="col-xs-3 blue" forFrame="scriptSection" initial="show" name="Panel1"></panel>
<panel classes="col-xs-3 white" forFrame="scriptSection" initial="hide" name="Panel2"></panel>
<panel classes="col-xs-3 blue" forFrame="scriptSection" initial="hide" name="Panel3"></panel>
<panel classes="col-xs-3 white" forFrame="scriptSection" initial="hide" name="Panel4"></panel>
<panel classes="col-xs-4 blue" forFrame="scriptSection" initial="hide" name="Panel5"></panel>
<panel classes="col-xs-4 white" forFrame="scriptSection" initial="hide" name="Panel6"></panel>
<panel classes="col-xs-4 blue" forFrame="scriptSection" initial="hide" name="Panel7"></panel>
<panel classes="col-xs-12 white" forFrame="scriptSection" initial="hide" name="Panel8"></panel>
</panels>
<elements>
// Buttons
<Button classes="btn-default col-xs-2 col-xs-offset-2 callControl" forFrame="callControl" id="transferCall" name="Transfer call" state=" ">
</Button>
<Button classes="btn-default col-xs-2 callControl" forFrame="callControl" id="holdCall" name="Place Call on hold" state=" ">
</Button>
<Button classes="btn-default col-xs-2 callControl" forFrame="callControl" id="EndCall" name="End call" state="">
</Button>
<Button classes="btn-default col-xs-2 callControl" forFrame="callControl" id="MakeNewCall" name="Make New Call" state="disabled">
</Button>
<Button classes="btn-success col-xs-2 col-xs-offset-2" id="ScriptSubmit" forFrame="ScriptSelectRow4" name="Select Script" state="">
</Button>
<Button classes="col-xs-1 col-xs-offset-10" forFrame="ScriptSelectRow5" id="ToLogin" name="Back" state="">
</Button>
<End></End>
</elements>
</script>`;
/* DOM Parsing*/
var parser0 = new DOMParser();
var xmlDoc = parser0.parseFromString(xmldata, "text/xml");
var tags =xmlDoc.getElementsByTagName('*');
$(document).ready(function() {
for (var i="0"; i <tags.length; i++){
switch (tags[i].nodeName){
case "panels":
break;
case "panel":
addPanel(tags[i].getAttribute("classes"), tags[i].getAttribute("forFrame"), tags[i].getAttribute("initial"), tags[i].getAttribute("name"));
break;
}
}
});
function addPanel(classes, forFrame, initial, name){
var a = '<section class="'+ classes + '" id ="' + name + 'Panel"><h2 class="text-center">' + name + '</h2><p id="' + name + 'Row1"></p><p id="' + name + 'Row2"></p><p id="' + name + 'Row3"></p></section>';
var b = '#' + forFrame;
var c = 'start' + name + initial;
var d = '<script id ="' + c + '"></script>';
var e = 'function ' + c + '(){';
var f = '$("' + name + 'Panel").' + initial + '();}';
$(b).append(a);
$(b).append(d);//fine
$(d).append(e);//inside script
$(d).append(f);//inside script
$(d).append(c);//inside/script
console.log(e + f + ' should be inside the script with id ' + d);
}
/*硬编码脚本来验证代码的正确性*/
<!DOCTYPE html>
<html lang=en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Logic Proof of concept with embedded XML</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<header class="row text-center">
<h1>Example Page to prove Logic capability</h1>
</header>
<main class="row" id="scriptSection">
</main>
<footer class="row">
<script src="javascript/jquery-3.1.0.min.js"></script>
<script src="javascript/bootstrap.min.js"></script>
<script src="javascript/logic.js"></script>
</footer>
</body>
</html>
在加载页面时,我没有得到控制台错误或警告,但到目前为止,已经设法将函数放到脚本标签之外的页面上,或者在函数代码上面的代码消失。
谁能看出我错在哪里
我已经将xml添加到对象中,并且它的行为正确scriptXML = JSON.parse(message).XML;
相关文章:
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 如何在DOM元素上按类型构建此函数
- 通过php页面中的js强制下载txt
- Windows形成web浏览器控件和Javascript更改的DOM
- Datatables:通过DOM数据源中的名称引用列
- 在DOM中查找一个模式并替换它's的内容使用jquery
- DOM事件通过JSON转换为java
- 将DOM节点值与字符串Javascript进行比较
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- 流星中DOM的繁殖
- DOM元素和angular元素之间的主要区别是什么
- 当带有渲染器的DOM元素不在屏幕顶部时,移动了场景的坐标
- 如何将Knockout.JS与服务器已经在DOM中呈现的数据同步
- PHP-如何重定向到同一页面并更改DOM's
- 如何使用ViewCompiler手动编译DOM的一部分
- 从popup.js|Chrome扩展访问DOM
- React DOM offsetHeight before rendering
- 我试图将动态脚本添加到页面中,但它只是在DOM中显示为txt