javascript - 如何在/从页面 B 调用和运行页面 A 的功能
问题描述
是否可以在页面中调用和运行函数,比如说a.html
从页面b.html
页面a.html
如下所示,具有更新页面背景颜色的功能
<!DOCTYPE html>
<html>
<head>
<title>Page A</title>
</head>
<body>
<h1>This is Page A</h1>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" referrerpolicy="no-referrer"></script>
<script>
function UpdatePageA(){
$("body").css("background-color","gold");
}
</script>
</body>
</html>
页面b.html
就像
<!DOCTYPE html>
<html>
<head>
<title>Page B</title>
</head>
<body>
<h1>Update Page A from Here</h1>
<button type="button" class="btn update-page-a">Update Page A</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" referrerpolicy="no-referrer"></script>
<script>
$(".update-page-a").on("click", function(){
UpdatePageA();
});
</script>
</body>
</html>
解决方案
此处的选项 4:https ://blog.bitsrc.io/4-ways-to-communicate-across-browser-tabs-in-realtime-e4f5f6cbedca
这是假设您打开了两个选项卡。如果您正在谈论从 a.html 到 b.html 的顺序移动,那么您可以使用查询字符串参数或本地存储。
推荐阅读
- rdf - 在 OWL 中使用 owl:equivalentClass 和子类
- c# - UWP Navigate 为无 xaml 页面引发 AccessViolationException
- terraform - 有没有办法合并 terraform 变量以跨多个 AWS 区域使用相同的模块?
- c# - c#替换列表字符串中的数字位置?
- c# - C# SqlCommand.ExecuteNonQuery() 和 SQL SET NOCOUNT ON
- spring-boot - 无法反序列化来自 Kafka 的数据
- css - wordpress 主题展示的全宽
- teamcity - 伪造脚本中的编译错误未在 TeamCity 构建失败摘要中显示特定失败
- sql-server - 子案例时避免多个案例
- sql-server - SQL Server 可以在连接中使用“like”吗?