Inserting a div element inside of an iframe

你离开我真会死。 提交于 2020-01-17 05:19:11

问题


I am trying to insert a div element inside of a iframe to which I would like to eventually apply a perfect scrollbar. As of now I have been able to insert the iframe inside div and apply perfect scrollbar to it. Any ideas?

My code is as follows:

<!DOCTYPE html>
<html>
  <head>
    <title>perfect-scrollbar</title>
    <link href="http://noraesae.github.io/perfect-scrollbar/bootstrap-combined.min.css" rel="stylesheet">
    <link href="http://noraesae.github.io/perfect-scrollbar/perfect-scrollbar.min.css" rel="stylesheet">

    <style>
      #Sidebar 
        {
          position:relative; 
          margin:0px auto; 
          padding:0px; 
          width: 230px; 
          height: 463px; 
          overflow: hidden;
        }
      #iframe
        {
          position: relative;  
        }

    </style>
  </head>
  <body>
    <div id='Sidebar'>                       
      <iframe id="iframe" src="http://www.w3schools.com/" width= "230" height="700" scrolling="no" frameborder="0">         
      </iframe>  
    </div> 
    <script src="http://noraesae.github.io/perfect-scrollbar/jquery.min.js"></script>
    <script src="http://noraesae.github.io/perfect-scrollbar/perfect-scrollbar.min.js"></script>
    <script src="http://noraesae.github.io/perfect-scrollbar/bootstrap.min.js"></script>        
    <script>
      $(function() {
        Ps.initialize(document.getElementById('Sidebar'));
        $('#iframe')     
        .contents().find('body')
        .append('<div id="sidebar"> Left Admin Panel </div>')
      });
    </script>
  </body>
</html>

The part I am trying to insert as a div inside iframe is as follows:

 <ul class="sidebar-menu">        
   <li class="header">MyOGI APPLICATIONS</li>
   <li class="treeview">
     <ul class="treeview-menu"></ul>
   </li>
   <li class="header">GLOBAL APPLICATIONS</li>
   <li class="treeview">
     <a href="#">
       <i class="fa fa-angle-left pull-right"></i>
     </a>
     <ul class="treeview-menu"></ul>
       <li class="treeview"></li>
       <li class="treeview">
          <ul class="treeview-menu"></ul>
       </li>
     </ul> 
   <li>
 <ul>

回答1:


$('#iframe')     
          .contents().find('body')
          .append('mynewcontent goes here');



回答2:


You cannot manipulate content in an iFrame you do not control. So, in most cases this is same-domain only. If it is another domain, you run into the cross-domain issue which is mostly unsolvable unless you own the other site as well



来源:https://stackoverflow.com/questions/29571365/inserting-a-div-element-inside-of-an-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!