简体   繁体   English

使用iframe加载来自其他HTML的内容

[英]Using Iframe to load content from another HTML

I need to display content(present in another HTML) in an iframe inside an already present HTML page. 我需要在已经存在的HTML页面内的iframe中显示内容(存在于另一个HTML中)。 The iframe should first check if there is some content in the HTML, and only load if there is, if the source HTML is empty the iframe should not load. iframe应首先检查HTML中是否有某些内容,并且仅在有HTML时加载,如果源HTML为空,则不应加载iframe。

Thank You 谢谢

To check wether the content is present in the iframe use this(it uses jQuery and let the id of iFrame be iframeid ): 要检查内容是否存在于iframe中,请使用此方法(它使用jQuery并让iFrame的id为iframeid ):

   <!--free.htm -->
    <!DOCTYPE html>
    <html>
      <head>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script>
         $(document).ready(function(){
             $.get("test.htm", function(){
                if($(this).contents().find("body").length<=0)  
                    $("#iframeid").css("display","none");       
             });
         });
         </script>
       </head>

       <body>
         <iframe src="test.htm" id="iframeid"></iframe>
       </body>
</html>

Here are dome links that might be of help to you : 以下是可能对您有所帮助的圆顶链接:

You'll have to use Javascript for this. 你必须使用Javascript。

Your parent HTML file: 您的父HTML文件:

<html>
  <!-- Your parent html file contents -->
  <iframe id="iframe" src="">

  </iframe>

  <script src="jquery-1.11.2.min.js"></script>
  <script>
    $(document).ready(function() {
      //get your iFrame html file
      $.get("iframe.html", function(data) {
          //check if file not empty
          if(data) {
             $("#iframe").attr("src", "iframe.html");
          } else {
             $("#iframe").css("display", "none");
          } 
      });
    });
  </script>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM