-1

我的索引.html

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>
<button onclick="document.location='target.html'" name="this" type="submit" value="hello" >target</button>
</body>
</html>

我的目标.html

<!DOCTYPE html>
<html>
<body>

<h1>2nd page</h1>


</body>
</html>

我想访问对 index.html 上单击按钮的值的访问权限,以便我可以在 target.html 页面上的任何位置使用它

在我的脑海中,它看起来像这样......

<!DOCTYPE html>

var buttonval = value of button click
<html>
<body>

<h1>2nd page</h1>


</body>
</html>

理想情况下,我想只使用 html 和 java 脚本来实现这一点

4

3 回答 3

2

您首先需要发送该值,因此根据您的代码,它将如下所示:

<button onclick="document.location='target.html?val=hello'" name="this" type="submit" value="hello" >target</button>

然后您需要在目标页面上恢复该值:

var url = new URL(window.location.href);
var val = url.searchParams.get("val");
于 2020-08-20T08:29:11.647 回答
1

您可以使用 GET 方法(Url 中的值)或 POST 方法(使用 <FORM)。

在这里获取:

索引.html:

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>
<button onclick="document.location='target.html?val=1234'" name="this" type="submit" value="hello" >target</button>
</body>
</html>

目标.html:

<!DOCTYPE html>
<html>
<body>
   <h1>2nd page</h1>    
   <div id="val"></div>
</body>

<script type="text/javascript">
    var parameters = location.search.substring(1).split("?");
    console.log(parameters);
    var vals = parameters[0].split("=");
    console.log(vals);
    document.getElementById("val").innerHTML = "value = "+vals[1];
</script>
</html>

在浏览器的控制台 (F12) 中查看信息。

很好的延续。 _泰迪熊_

于 2020-08-20T08:53:54.913 回答
0

使用页面包括使用 PHP

example : <?php include("page.php"); ?>

在可见文件中执行单击直接。

于 2020-08-20T08:32:58.497 回答