我正在尝试将动态脚本添加到页面中,但它只是在DOM

时间:2016-09-02 13:26:48

标签: javascript jquery html

我使用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);
}

/ *用于验证代码正确的硬编码脚本* /

这是html;

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

在加载页面时,我没有得到控制台错误或警告,但到目前为止已经过纳道操作以将函数放到脚本标记之外的页面上,或者使用功能代码上方的代码消失。

任何人都可以看到我出错的地方

1 个答案:

答案 0 :(得分:1)

我已将xml添加到对象中,并且行为正确 scriptXML = JSON.parse(message).XML;因为它现在是来自服务器的JSON响应的一部分。