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

Javascript重定向到页面并动态修改它

Javascript重定向到页面并动态修改它,javascript,html,Javascript,Html,我正在尝试的是: 我正在单击一个按钮继续,它会将我带到另一个html页面 然后,我将基于我的数组创建一个动态表 但页面不显示该表。 注意:我只想通过JS实现这一点,而不是通过Jquery或任何其他框架 代码: 按下“继续”按钮时,将调用下面的函数 function Proceed(){ window.location.href = '\cart.html'; function addItems(){ var cartTable = document.crea

我正在尝试的是:

  • 我正在单击一个按钮继续,它会将我带到另一个html页面
  • 然后,我将基于我的数组创建一个动态表
  • 但页面不显示该表。 注意:我只想通过JS实现这一点,而不是通过Jquery或任何其他框架

    代码:

    按下“继续”按钮时,将调用下面的函数

    function Proceed(){
    
        window.location.href = '\cart.html';
    
        function addItems(){
    
            var cartTable = document.createElement("table");
    
            var tr1 = document.createElement("tr");
    
            tr1.appendChild(createElement("td"));
            tr1.appendChild(createElement("td"));
            tr1.appendChild(createElement("td"));
    
            tr1.cells[0].appendChild( document.createTextNode('Sr. Number'));
            tr1.cells[0].appendChild( document.createTextNode('Item'));
            tr1.cells[0].appendChild( document.createTextNode('Price'));
    
            cartTable.appendChild(tr1);
    
            for(i=0; i<cartCount; i++){
    
                var cartItem = myCart[i];
    
                var tr = document.createElement("tr");
    
                tr.appendChild(createElement("td"));
                tr.appendChild(createElement("td"));
                tr.appendChild(createElement("td"));
    
                tr.cells[0].appendChild( document.createTextNode(i+1));
                tr.cells[0].appendChild( document.createTextNode(cartItem.Name));
                tr.cells[0].appendChild( document.createTextNode(cartItem.Price));
    
                cartTable.appendChild(tr);
    
            }
    
            document.getElementByID(finalCart).innerHTML = cartTable;
        }
    }
    
    函数继续(){
    window.location.href='\cart.html';
    函数addItems(){
    var cartTable=document.createElement(“表”);
    var tr1=document.createElement(“tr”);
    tr1.appendChild(createElement(“td”));
    tr1.appendChild(createElement(“td”));
    tr1.appendChild(createElement(“td”));
    tr1.cells[0].appendChild(document.createTextNode('Sr.Number');
    tr1.cells[0].appendChild(document.createTextNode('Item');
    tr1.cells[0].appendChild(document.createTextNode('Price');
    cartTable.appendChild(tr1);
    
    对于(i=0;i),您不能离开某个页面,然后在刚刚离开的页面上继续运行脚本

    相反,您可以先生成新页面的html,然后将其转换为可以重定向或链接到的dataURI(因为这是在一个SO代码段中,所以我无法自动将新窗口定向或重定向到它,所以我只是生成一个直接链接;在现实生活中,您不会有这种限制)

    函数继续(){
    /*因为这些都将被转换成字符串,
    只将html创建为字符串会更容易
    使用DOM方法。为了不在周围乱搞
    对于您现有的代码,我将保持原样
    使用.outerHTML提取html字符串*/
    var cartTable=document.createElement(“表”);
    var tr1=document.createElement(“tr”);
    tr1.appendChild(document.createElement(“td”);
    tr1.appendChild(document.createElement(“td”);
    tr1.appendChild(document.createElement(“td”);
    tr1.cells[0].appendChild(document.createTextNode('Sr.Number');
    tr1.cells[1].appendChild(document.createTextNode('Item');
    tr1.cells[2].appendChild(document.createTextNode('Price');
    cartTable.appendChild(tr1);
    /*表的其余部分等*/
    //创建数据URI:
    var generatedLink=“data:text/html;base64,”+window.btoa(cartTable.outerHTML);
    //创建指向它的链接:
    document.getElementById('output').innerHTML=“”;
    }
    
    生成链接
    
    东西不是这样工作的。一旦你更改了页面,浏览器就会接管,当前页面上的JS代码就不再执行了。你需要的是要导航的页面上的JS,而不是要导航离开的页面上的JS。因为你要导航离开该页面。如果你真的需要导航到另一个页面并持久化该页面,则ata,您可以将其存储在cookie中,以便下一页的javascript读取。谢谢Daniel和所有其他人。我将进行更改:)