change iframe content jquery

后端 未结 3 683
半阙折子戏
半阙折子戏 2021-01-03 11:28

I\'d like to change my iframe content but I don\'t understand how


    
        test
        

        
相关标签:
3条回答
  • 2021-01-03 11:30
    <script type="text/javascript">
      $(document).ready(function(){        
           $('#frame').on('load', function() {
               $(this).contents().find( "body" ).html('<p>hi</p>');
           });
      });
    </script>
    

    While this is right, the content will never change as you are trying to modify the body of an external resource. You cannot do this due to cross-site scripting rules.

    EDIT:

    The only way to do it is to do what @user1153551 did and replace the whole document.

    0 讨论(0)
  • 2021-01-03 11:33

    use document.ready

    <script type=text/javascript >
      $(document).ready(function(){        
          $('#frame').on('load', function() {
           $(this).contents().find( "body" ).html('<p>hi</p>');
         });
      });
    </script>
    
    0 讨论(0)
  • 2021-01-03 11:43

    Check this Demo jsFiddle

    you have to use load event within the iframe's document and calling out to a load function in the containing document to replace to a body contain.

    jQuery

    $('#frame').load(function() {
      $('#frame').replaceWith('<p>hi</p>');
    });
    

    HTML

    <iframe id="frame" width="100%" height="95%" src="http://w3schools.com"></iframe>
    

    Use .replaceWith() jQuery Method.

    0 讨论(0)
提交回复
热议问题