gpt4 book ai didi

javascript - iframe 中的按钮应触发事件

转载 作者:行者123 更新时间:2023-11-28 05:46:05 25 4
gpt4 key购买 nike

我在 iframe 中有一个按钮。我希望该按钮触发 iframe 外部的事件。我怎样才能做到这一点?

我有以下按钮

<button class="btn">Back</button>

在 iframe 内

我希望当人们单击该按钮时,它会在 iframe 外部的 View 中触发一个事件。我怎样才能做到这一点?

无需担心跨域,因为 iframe url 与 View 位于同一域中。

该按钮仅在用户导航到 iframe 的第二页时显示。

最佳答案

它不能,至少不能直接。当事件到达文档顶部时,事件将停止冒泡。

你可以这样做:

button.addEventListener("click", passEventToParent);

function passEventToParent(event) {
parent.querySelector("iframe").dispatchEvent(event);
}

See dispatchEvent on MDN for more information about it .

关于javascript - iframe 中的按钮应触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com