在这里,我尝试将数据从一页发送到另一HTML页。我尝试使用window.load方法和localStorage,但无法获取输出。当单击按钮时,我有一个带有标题和按钮的表,当前行标题将显示在b.html页面中。谁能向我建议正确的方向。
a.html
<body>
<table class="at-items-block">
<thead></thead>
<tbody>
<tr>
<td class="at-item">
<h2 class="at-title">Title1 Here</h2>
</td>
<td class="at-item-btn"><a href="b.html">send</a></td>
</tr>
<tr>
<td class="at-item">
<h2 class="at-title">Title2 Here</h2>
</td>
<td class="at-item-btn"><a href="b.html">send</a></td>
</tr>
</tbody>
</table>
</body>
<script>
window.onload = function() {
var getTitle = document.getElementsByClassName("at-title");
localStorage.setItem("itemsTitle",getTitle);
}
</script>
b.html
<div class="titleFetch"></div>
</body>
<script>
window.onload = document.getElementsByClassName("titleFetch").innerHTML =
localStorage.getItem("itemsTitle");
</script>
我认为最好的方法是使用PHP-GET方法(例如在PHP中使用方法)
$var = $_GET['mode']
//to Use this you have to change your link, for example b.html?mode=YourString
//here you have the string right from the '='-character
或者,如果您想将此方法与JavaScript一起使用,则只能使用此方法
let endingString = "";
let workingString = window.location.href;
let boolpath = false;
for(var i = 0; i < workingString.length; i++)
{
if (boolpath)
{
endingString += workingString[i];
}
else if (workingString[i] == '?') {
boolpath = true;
}
}
//to Use this you have to change your link, for example b.html?hallo123
//here you have the complete string in the right of the '?'-mark
//b.html?hallo123 -> endingString="hallo123"
我个人推荐JavaScript方法
使用Cookies(Javascript)将数据从一页发送到html页中的另一页。这是在多页上保存和发送数据的简便方法。访问https://www.w3schools.com/js/js_cookies.asp