Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery单击不影响window.onload_Javascript_Jquery_Iframe - Fatal编程技术网

Javascript JQuery单击不影响window.onload

Javascript JQuery单击不影响window.onload,javascript,jquery,iframe,Javascript,Jquery,Iframe,我无法启动窗口。当我单击时,onload事件似乎对我不起作用 例如,当我单击复选框时,他们将调用函数来呈现带有内容的iframe。如果取消注释第2行callOnLoad(),iframe将加载内容,但这不是我所期望的,我期望选中复选框时,iframe中的内容将被呈现 我假设您想要以下内容:- iframe将在页面加载(或窗口加载)时显示,但选中/取消选中复选框时文本会出现/消失? 如果是,请执行以下操作:- <script src="https://ajax.googleapis.com/

我无法启动
窗口。当我单击时,onload
事件似乎对我不起作用

例如,当我单击复选框时,他们将调用函数来呈现带有内容的iframe。如果取消注释第2行
callOnLoad()
,iframe将加载内容,但这不是我所期望的,我期望选中复选框时,iframe中的内容将被呈现


我假设您想要以下内容:-

iframe将在页面加载(或窗口加载)时显示,但选中/取消选中复选框时文本会出现/消失?

如果是,请执行以下操作:-

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    <input type="checkbox" id="cgv" />
        <div id="foo"></div>
</body>

<script>
$(document).ready(function() {
    callOnLoad();
    $('#cgv').on('click', function(e) {
        if ($('#cgv:checked').length !== 0) {
            $('#iframe').contents().find("body").append('Test <br/>' + new Date());
        } else {
            $('#iframe').contents().find("body").html('');
        }
    });
});
// Function render iframe
function callOnLoad() {
    $("#foo").append("<div id=\"div-parent\"><iframe id=\"iframe\"></iframe></div>");
}
</script>

$(文档).ready(函数(){
callOnLoad();
$('cgv')。在('click',函数(e)上{
如果($('#cgv:checked')。长度!==0){
$('iframe').contents().find(“body”).append('Test
'+new Date()); }否则{ $('#iframe').contents().find(“body”).html(“”); } }); }); //函数渲染iframe 函数callOnLoad(){ $(“#foo”)。附加(“”); }
检查

JS:

//在此处添加您的javascript
$(函数(){
$('cgv')。在('click',函数(e)上{
如果($('#cgv:checked')。长度!==0){
callOnLoad();//在此处加载iframe,iframe不工作,内容不呈现
}否则{
$('#div parent').remove();//remove div,
}
});
});
//函数渲染iframe
函数callOnLoad(){
$(“#foo”)。附加(“”);
var iframe=$(“#foo”).find('[id=“iframe”]');
iframe.contents().find(“body”).append('Test
'+new Date()); }
HTML:



所以您希望在窗口加载时显示iframe,并在单击复选框时显示文本?如果单击复选框,则不会出现事件加载。只有当你进入页面时才会触发onload。谢谢RonyLoud,我还没有测试它,但这是处理我的问题的一个非常聪明的方法…但问题是这个iframe是从另一个URL呈现的,我们不能附加任何内容,这将导致跨上下文iframe…bla-bla…谢谢
活到死
,你的昵称让我害怕…哈哈…这是处理我的问题的一个非常聪明的方法,但问题是iframe不是从我的服务器渲染的,它是从另一个源渲染的,我们无法编辑或更改iframe,这将导致
阻止一个源为“我的服务器url”的帧从访问跨原点帧。
@MikeNguyen如果iframe通过第三方传输,则您无法执行任何操作。对不起,但这是真的
// Add your javascript here
$(function() {
  $('#cgv').on('click', function(e) {
    if ($('#cgv:checked').length !== 0) {
      callOnLoad(); // Load iframe here, iframe does not work, the content does not render
    } else {
      $('#div-parent').remove(); // Remove div,
    }
  });
});

// Function render iframe
function callOnLoad() {
  $("#foo").append("<div id=\"div-parent\"><iframe id=\"iframe\" class=\"active\"></iframe></div>");
  var iframe = $("#foo").find('[id="iframe"]');
iframe.contents().find("body").append('Test <br/>' + new Date());

}
<input type="checkbox" id="cgv" />
  <div id="foo">
  </div>