如何创建iframe内容的书签

How to create the bookmark of the iframe content

本文关键字:书签 iframe 何创建 创建      更新时间:2024-04-13

在这个例子中,我得到了一个id为"about_ma"的列表元素和一个名称为"page"的iframe,但它最初没有显示。

<div id="navigation">
          <ul id="nav">
      <li id="about_ma"><a href="index1.php?name=about.php">About Us</a></li>
      <li><a href="index1.php?name=director.php">Director Desk</a></li>
      </ul>
 </div>
 <iframe src="<?php echo $name;?>"style="display:none;"name="page" id="nav_content">
 </iframe>  

我使用以下php代码在url 中获取名称

   <?php
   $name= $_GET['name'];
   echo "<br>";
   echo $name;
   ?>`

我使用的脚本是

 $(document).ready(function(){
    $('#about_ma').click(function(){
           $('#nav_content').fadeIn();
  });
  });

如果我的概念是正确的,那么根据我的说法,当我点击"关于我们"时,应该会显示带有src About.php的iframe。整个编码是在index1.php上完成的。所以有人能告诉我这个错误吗?因为我试过了,但没有效果。编码之前的主要目的是创建iframe内容的书签。如果我的概念是错误的,那么有人能告诉我如何创建iframe内容的书签吗。因为当你刷新整个网页时,会加载带有默认src的iframe,因此无法为iframe内容添加书签。请帮帮我。

如果您想在单击导航链接时更改iframe的src,则需要使用attr()方法对其进行修改。为了能够添加书签,您需要使用哈希。将在链接的data-属性中存储一个哈希名称。

HTML

<li id="about_ma"><a href="index1.php?name=about.php" data-hash="about">About Us</a></li>

JS-

$('#navigation a').click(function(e){
       e.preventDefault();/* prevent browser opening link*/
       location.hash=$(this).data('hash');/* set hash of url*/
       var newSrc=$(this).attr('href');
       $('#nav_content').attr('src', newSrc).fadeIn(); 
    });
/* on page load*/
var hash=location.hash;
if(hash && hash !='#'){
  hash=hash.slice(1)
  $('#navigation a[data-hash='+hash+']').click()
}