简体   繁体   English

我使用了 HTML + JAVAScript,但我只能看到 JAVAScript

[英]I used HTML + JAVAScript, But i can see only JAVAScript

I used HTML and JAVAscript in one page.我在一页中使用了 HTML 和 JAVAscript。

But when i enter the site, and see.但是当我进入网站时,看看。

I can see only JAVAscript, not HTML source.我只能看到 JAVAscript,而不是 HTML 源。

There are few line of HTML string, and one jaascript which creat table有几行 HTML 字符串,以及一个创建表的 jaascript

In the page, i can see only table, not few of HTML string在页面中,我只能看到表格,而不是 HTML 字符串

In the body code below,在下面的正文代码中,

Only i can see "script ~~/script" Only.只有我可以看到“脚本 ~~/script”。 I can't see this below in the web page.我在 web 页面中看不到这个。

    <br>
   <a href="./admin.html">Back to admin main page</a> <br>
   <a href="./food_list.html">Go to food list </a> <br>
   <a href="./food_detail_list.html">Go to food detail list </a> <br>

This is the body code这是正文代码

    <body>
         <br>
        <a href="./admin.html">Back to admin main page</a> <br>
        <a href="./food_list.html">Go to food list </a> <br>
        <a href="./food_detail_list.html">Go to food detail list </a> <br>

        <script>showFoodList();</script>

    </body>

this is a whole code这是一个完整的代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>

        <script language="javascript">

            // this function is used to ask food to server and take a data with json
            function showFoodList(){
                var para = document.location.search;
                fetch("http://gyrjs07.dothome.co.kr/what_to_eat/foodlist.php")
                .then(function(response) {
                    return response.json();
                })
                .then(function(myJson) {
                    creatTable(myJson);
                });

                }

            function creatTable(data){
                document.write("<table width='80%' border='1' style='margin: auto; text-align: center'>");

                // for문 시작
                for ( var i=0; i<data.length ; i++) {
                    document.write("<tr>");    // <tr> : 행추가
                        // 이중 for문 시작
                        for ( var j=0; j<data[i].length ; j++) {
                            var txt=data[i][j];    // 테이블각 셀에값을 출력
                            document.write("<td>"+ txt +"</td>");     // <td> : 열추가.
                            }
                    document.write("</tr>");
                } //end for i
                document.write("</table>");   // 테이블 태그를 닫는다.
            }

        </script>
    </head>   

    <body>
         <br>
        <a href="./admin.html">Back to admin main page</a> <br>
        <a href="./food_list.html">Go to food list </a> <br>
        <a href="./food_detail_list.html">Go to food detail list </a> <br>

        <script>showFoodList()</script>;

    </body>
</html>

It happens because you use document.write method.发生这种情况是因为您使用了 document.write 方法。 It automatically calls document.open which removes all existing nodes in a document.它会自动调用 document.open 来删除文档中的所有现有节点。 Documentation forDocument.write() and Document.open() Document.write()Document.open()的文档

Better approach is append children to some predefined node as it shown in snippet below.更好的方法是 append 子节点到某个预定义节点,如下面的片段所示。

 const fakeApiResponse = [ ["number", "name", "q-ty"], ["1.", "soda", "2"], ["2.", "beer", "3"], ["3", "pizza", "3"] ]; // this function is used to ask food to server and take a data with json function showFoodList() { // Emulate api call to get data Promise.resolve(fakeApiResponse).then(function(myJson) { creatTable(myJson); }); } function creatTable(data) { // Get mountig node from HTML const mountingPoind = document.getElementById("food-list"); // Create Table element and set some attributes const table = document.createElement("table"); table.setAttribute("width", "80%"); table.setAttribute("border", "1"); table.setAttribute("style", "margin: auto; text-align: center"); // Start iterate over data to create table structure for (let i = 0; i < data.length; i++) { createTableRow(table, data[i]); } // Append table structure to mounting point mountingPoind.appendChild(table); } // Creates row. Add text call createCell function to generate cells with data // and append it to parent function createTableRow(parent, rowData) { const row = document.createElement("tr"); for (let j = 0; j < rowData.length; j++) { createCell(row, rowData[j]); } parent.appendChild(row); } // Creates cell. Add text data to it and append it to parent function createCell(parent, cellData) { const cell = document.createElement("td"); cell.innerText = cellData; parent.appendChild(cell); } showFoodList();
 <.DOCTYPE html> <html> <head> <title>Parcel Sandbox</title> <meta charset="UTF-8" /> </head> <body> <br /> <a href="./admin.html">Back to admin main page</a> <br /> <a href="./food_list.html">Go to food list </a> <br /> <a href="./food_detail_list.html">Go to food detail list </a> <br /> <br /> <div id="food-list"></div> <script src="src/index.js"></script> </body> </html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以动态设置mailto的收件人:仅根据访问网站的URL使用HTML和JavaScript吗? - Can I dynamically set the recipient of mailto: using only HTML and JavaScript depending on URL used to access the site? 如何通过 devtools(chrome、Edge 等)查看 JavaScript 使用的内存中的哪些变量? - How can I see which variables are specifically in memory used by JavaScript through devtools (chrome, Edge, etc.)? Javascript - 我看不到值总和的结果 - Javascript - I can not see the result of the sum of values 如何查看包含的 JavaScript 源代码? - How can I see included JavaScript sources? Fusionchart已加载,但我看不到-javascript - Fusionchart is loaded but i can't see it - javascript JavaScript验证无效-我看不到任何错误 - Javascript validation not working - no error I can see 我可以得到 xml 标记值用于 html,还是可以使用<ul> &amp;<il> 在 javascript 中?</il></ul> - can I get xml tag value to be used in in html, or can I use <ul> & <il> in javascript? 在 javascript 中,如何检查 textarea 是否只有一行文本? - In javascript, how can I check to see if a textarea has only one line worth of text? 我似乎看不到我的 JavaScript 和 HTML 代码中缺少什么 - I can't seem to see what's missing in my JavaScript and HTML code 我如何通过使用HTML,CSS和JavaScript来实现这种效果(请参阅内部)? - How can I do effect like this(see inside) by using HTML, CSS and JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM