我想在 iFrame 中的按钮上生成点击事件,控制台消息说它找到了按钮,并点击了它,但我没有看到任何反应。
如果我用手点击 iframe 中的下拉箭头,它会显示一个弹出菜单,但如果我从父框架调用 runExcel() 函数,它会显示它被点击,但找不到弹出菜单内容。也许 iframe 中的按钮使用自定义点击事件?如果是这样,是否有任何其他方法可以在按钮上产生真正的点击? (我试过点击事件,它也不起作用。)
谢谢
这是函数:
function runExcel() {
var iframe= document.getElementsByTagName("iframe")[0].contentWindow;
var Report0 = iframe.document.getElementsByTagName("iframe")[0].contentWindow;
var Report1 = iframe.document.getElementsByTagName("iframe")[1].contentWindow;
/ Find the "Run Excel" menu item
setTimeout(() => {
const runExcelItem = Report1.document.querySelector('#mnuAppToolbarLeftPaneRun_spreadsheetML');
// Click on the "Run Excel" menu item to run the report
if (!!runExcelItem) {
// Dispatch native MouseEvents for the "Run Excel" menu item
runExcelItem.dispatchEvent(nativeMouseDownEvent);
runExcelItem.dispatchEvent(nativeMouseUpEvent);
console.log('Clicked "Run Excel" menu item');
} else {
console.log('Could not find "Run Excel" menu item');
}
}, 1000);
}, 1000);
} else {
console.log('Could not find dropdown arrow');
}
}
and in my parent frame the button is:
<button type="button" onclick="runExcel();">Run Excel</button>
控制台:
也许您应该将您的变量(Report0、Report1)添加到 console.Log() 中,以准确查看您找到的内容。 大概应该这样写:
let iframe1 = document.getElementsByTagName("iframe")[0];
let iframe2 = document.getElementsByTagName("iframe")[1];
let Report1 = iframe2.contentWindow.document.querySelector('#mnuAppToolbarLeftPaneRun_spreadsheetML');