我想做的是:
但佩奇没有显示表格。注意:我只想通过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;
}
}
发布于 2017-05-23 21:19:48
您不能离开页面导航,然后继续在您刚刚离开的页面上运行脚本。
相反,您可以首先生成新页面的html,然后将其转换为您可以重定向或链接到的dataURI。(因为这是在一个非常小的片段中,所以我不能针对一个新窗口或自动地重定向到它,所以我只是生成一个直接链接;在现实生活中,您不会有这个限制。)
function Proceed() {
/* since this is all going to be converted to a string anyway,
it would be easier to just create the html as a string instead
of using DOM methods. In the interest of not mucking around too
much with your existing code I'm leaving that as is and then
extracting the html string with .outerHTML. */
var cartTable = document.createElement("table");
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);
/* Etcetera for remainder of table */
// create the data URI:
var generatedLink = "data:text/html;base64," + window.btoa(cartTable.outerHTML);
// create a link to it:
document.getElementById('output').innerHTML = "<a href='" + generatedLink + "'>Go to generated page</a>";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="Proceed()">Generate Link</button>
<div id="output"></div>
(不用说,您可能最好将这个服务器端生成为一个普通的HTML页面,但是如果您需要保留客户端,这应该是可行的。)
https://stackoverflow.com/questions/44143113
复制相似问题