javascript - 使用 jquery 或 javascript 在外部 iframe 中设置输入字段值
问题描述
我强烈面临 iframe 问题。我需要在其他 web 端的输入字段中设置值。我发布我的代码。我已经在检查contentDocument(返回 null)和contentWindow不起作用(捕获错误)任何人都可以帮助我。
我正在关注链接https://www.w3schools.com/jsref/prop_frame_contentwindow.asp
但我没有成功。当我检查console.log($('[name=name]'))
浏览器控制台减损时,console.log($('[name=name]').value=4)
然后使用错误。
当浏览器控制台测试使用相同的代码($('[name=name]').value=4
)时,有时会设置值,有时会出错。
用于测试的完整代码发布
<html>
<head>
<style>
iframe{
border: none;
width: 100%;
height: 100%;
}
</style>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<!-- <script src="script/jquery-1.10.2.js?id=12"></script> -->
<script>
window.onload=function(){
$("#btnLoad").on("click",function(){
document.getElementById('ifUrl').src=$("#txtUrl").val();
$("#btnFill").prop('disabled', true);
document.getElementById("ifUrl").onload=function(){
setTimeout(function(){ $("#btnFill").prop('disabled', false); }, 5000)
$("#btnFill").on("click",function(){
var iframe = document.getElementById("ifUrl");
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
//var text = $("#ifUrl").contents();
console.log(innerDoc);
//console.log($('#ifUrl'))
});
};
//alert($("#txtUrl").val());
// $("#ifUrl").load(function() {
// alert()
// //alert($(this).contents().find("html"))
// });
});
}
</script>
</head>
<body>
<div>URL:
<input type="text" id="txtUrl" value="https://www.dsebd.org/contact.php"/>
</div>
<div>
<button id="btnLoad" type="button">Load Url</button>
<button id="btnFill" type="button" disabled="true">Fill Up</button>
</div>
<div>
<iframe id="ifUrl">
</iframe>
</div>
</body>
解决方案
推荐阅读
- compiler-construction - AST 设计 - 增量运算符是语句还是表达式?
- python - 自定义 django-allauth 社交登录和回调 URL(通过前端服务重定向它们)
- javascript - Javascript文件如何从JSON文件中获取数据
- android - 当另一个活动完成时如何更新片段的回收视图?
- python - 试图创建一个循环,使用旧目录中的修改文件创建一个新目录
- javascript - 执行不等式和多个“数组包含”或类似数组包含的过滤器
- java - 为什么我在这个程序中得到了 IndexOutOfBoundException?
- c++ - C++ 组合向量的元素
- rx-swift - 在 BehaviourRelay 中更新嵌套属性的最佳方法
- php - 查询中的 LIMIT 和 OFFSET