javascript - 如何从 Iframe 内部调用 html 点击事件的服务器端函数?
问题描述
解决方案
您不能直接调用服务器端函数。但是使用一些简单的 jQuery 是可能的。
在包含 iframe 的页面上放置以下代码
<iframe src="index.html" width="200" height="100" frameborder="1"></iframe>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static" OnClick="Button1_Click" />
<script type="text/javascript">
function executeIframeCode() {
$('#Button1').click();
}
</script>
然后在 iframe 中放置一个按钮或任何您想要的并executeIframeCode
在父级中调用该函数。
<button onclick="parent.executeIframeCode()" type="button">
Execute Parent Code
</button>
代码背后的父代码
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Called from Iframe!";
}
这仅适用于 iframe 页面位于同一域中的情况。
推荐阅读
- reactjs - 将对象从子状态传递给父状态
- r - 是否可以在 for 循环中使用 aov 函数?
- react-native - React Native Android 调试构建在启动时崩溃,iOS 工作正常
- java - 我无法创建一种方法来计算出现次数
- api - Laravel 5.8 休息客户端如何在 .env 中保存 api 令牌
- spring-boot - 如何使用 Spring boot 在 OAuth2.0 中添加用于授权和身份验证的自定义逻辑?
- android - 为什么 setOnclicklistener 不能在无尽回收器视图中的特定行中工作?
- python - 如果在目标表中找不到记录(与源具有相同的索引号),则比较两个数据帧(源与目标)并留空行
- codenameone - 允许您指定 iOS 构建的构建提示
- registry - 使用 Inno Setup 安装 mod/plugin 时如何从注册表中获取目标游戏/应用程序的安装路径?