使用JavaScript将JavaScript代码添加到HTML页面

时间:2018-07-30 21:23:14

标签: javascript html innerhtml appendchild

我有一个HTML表单,该表单采用字段值来输出自定义指令HTML和JavaScript指令。

当用户填写表单并提交时,将执行JS代码以将指令插入页面底部。一旦包含<script>,脚本就会“死亡”,并且按钮将停止工作。我不明白发生了什么。

下面是我的代码。让我知道您是否还有其他需要。

<html>
<head>
    <script type="text/javascript">
        function formInfo(personalCode)
        {
            var instructionHttp = "<p># Step 1 - Add " + personalCode + "'s API</p><pre><script src='https://" + personalCode + ".customapi.com'></script></pre>";
            document.getElementById('returnedHtml').innerHTML = instructionHttp;
        }
    </script>
</head>
<body>
    <h1>Instructions Generator</h1>
    <form name="myform" action="" method="GET">
        <div class="showLabel"><label for="signupcode">Code</label></div><input type="text" name="signupcode" value="" autofocus ><br>
        <input type="button" value="submit" onclick="
            code=document.myform.signupcode.value;
            formInfo(signupcode);"> <br>
    </form>
    <br><hr><br>
    <p>--- Customized Instructions ---</p>
    <br>
    <div id="returnedHtml"></div><br>
</body>

我也尝试过使用以下内容替换formInfo()

    <script type="text/javascript">
        function formInfo(personalCode)
        {
            var pre1 = document.createElement('pre');
            var api = document.createElement('script');
            api.src = "https://" + personalCode + ".customapi.com";
            pre1.appendChild(api);

            var instructionHttp = "<p># Step 1 - Add " + personalCode + "'s API</p>";
            instructionHttp.appendChild(pre1);

            document.getElementById('returnedHtml').innerHTML = instructionHttp;
        }
    </script>

2 个答案:

答案 0 :(得分:0)

您不能使用HTML创建元素;您只能通过其标签名称创建一个元素(可以使用const clinics = { "a0CW00000027OX3MAM": { "id": "a0CW00000027OX3MAM", "companyName": "Hendrick Medical Center", "startTime": "2018-08-10T05:30:00.000Z", }, "a0CW00000026gjJMAQ": { "id": "a0CW00000026gjJMAQ", "companyName": "ABC Manufacturing", "startTime": "2018-08-10T10:36:00.000Z", }, "a0CW00000026gipMAA": { "id": "a0CW00000026gipMAA", "companyName": "ABC Manufacturing", "startTime": "2018-08-01T10:36:00.000Z", } } function compare(a,b) { if (a.startTime < b.startTime) return -1; if (a.startTime > b.startTime) return 1; return 0; } function objectToArray(obj){ var keys = Object.keys(obj); var ret = []; for (var i = 0; i < keys.length; i++) ret.push(obj[keys[i]]); return ret; } function arrayToObject(arr){ var ret = {}; for(var i = 0; i < arr.length; i++) ret[arr[i].id] = arr[i]; return ret; } var arr = objectToArray(clinics); console.log(arr.sort(compare)); console.log(arrayToObject(arr));属性设置其文本)以附加到DOM元素。

textContent

答案 1 :(得分:0)

您试图从字符串创建html并将其追加到元素,这是错误的,您必须像使用scriptpre一样创建元素。

此代码将为您工作:

<html>
    <head>
        <script type="text/javascript">
            function formInfo()
            {
               var personalCode = document.getElementById('signupcode').value
               var pre1 = document.createElement('pre');
               var api = document.createElement('script');
               api.src = "https://" + personalCode + ".customapi.com";
               pre1.appendChild(api);

               var instructionHttp = document.createElement('p');
               instructionHttp.innerHTML = "# Step 1 - Add " + personalCode + "'s API"

               instructionHttp.appendChild(pre1);

               document.getElementById('returnedHtml').innerHTML = instructionHttp.outerHTML;
           }
       </script>
   </head>
   <body>
       <h1>Instructions Generator</h1>
       <form name="myform" action="" method="GET">
           <div class="showLabel"><label for="signupcode">Code</label></div><input type="text" name="signupcode" id="signupcode" value="" autofocus ><br>
           <input type="button" value="submit" onclick="formInfo()"> <br>
       </form>
       <br><hr><br>
       <p>--- Customized Instructions ---</p>
       <br>
       <div id="returnedHtml"></div><br>
   </body>
</html>