使用jquery或javascript在外部iframe内设置输入字段值
我强烈地面临iframe问题。我需要在另一个web端的输入字段中设置值。我发布我的代码。我已经在检查contentDocument(返回null)和contentWindow不工作(捕捉错误)任何人都可以帮助我 我在跟踪链接 但我没有成功。当我检查使用jquery或javascript在外部iframe内设置输入字段值,javascript,jquery,Javascript,Jquery,我强烈地面临iframe问题。我需要在另一个web端的输入字段中设置值。我发布我的代码。我已经在检查contentDocument(返回null)和contentWindow不工作(捕捉错误)任何人都可以帮助我 我在跟踪链接 但我没有成功。当我检查console.log($('[name=name]'))browser-console-detract时,使用console.log($('[name=name]')).value=4) 当浏览器控制台测试使用相同的代码($('[name=name]
console.log($('[name=name]'))
browser-console-detract时,使用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>
iframe{
边界:无;
宽度:100%;
身高:100%;
}
window.onload=function(){
$(“#btnLoad”)。在(“单击”,函数(){
document.getElementById('ifUrl').src=$(“#txtUrl”).val();
$(“#btnFill”).prop('disabled',true);
document.getElementById(“ifUrl”).onload=function(){
setTimeout(function(){$(“#btnFill”).prop('disabled',false);},5000)
$(“#btnFill”)。在(“单击”,函数(){
var iframe=document.getElementById(“ifUrl”);
var innerDoc=iframe.contentDocument | | iframe.contentWindow.document;
//var text=$(“#ifUrl”).contents();
console.log(innerDoc);
//console.log($('#ifUrl'))
});
};
//警报($(“#txtUrl”).val());
//$(“#ifUrl”).load(函数(){
//警报()
////警报($(this).contents().find(“html”))
// });
});
}
网址:
加载Url
加满
如果您查看控制台,您可以阅读“阻止具有原点的帧…访问跨原点帧”。请查看感谢您的宝贵链接。我将阅读此内容。如果您提供解决方案或参考链接,我将很高兴与您联系