English 中文(简体)
2. 动态改变主角来源
原标题:Script to dynamically change the source of an iframe tag

我希望,如果一顶超级链接被点击,我就有一个总页,在被点击超级链接时,应当使用iframe tag从其他网页上装载内容,但我不知道如何这样做,那么该代码将动态地改变机面的来源。 我的法典

<html>
  <head>
    <title>
    </title>

    <style type="text/css">
      #content {
        margin-right:30%;
        margin-bottom: 10px;
        padding: 10px;
        border: 1px solid #000;
        margin-left: 17%;
        background-repeat: no-repeat;
        width: 686;
        height: 640;
      }
    </style>

    <script type="text/javascript">

      function hyperlinkChanger(hyperLink) 
      {

        switch (hyperLink)
        {

          case "home":
            var url="home.html";
            return url;
            break;

          case "about_Us":
            var url="about_us.html";
            return url;

            break;

          case "contact_Us:
            var url="contact_us.html";
            return url;

            break;
        }
      }
    </script>
  </head>
  <body>
    <a href="home.html" name="home" onClick="hyperlinkChanger( home )">Home</a>
    <br />
    <a href="about_us.html" name="about_Us" onClick="hyperlinkChanger( about_Us )">About Us</a>
    <br />
    <a href="contact_us.html" name="contact_Us" onClick="hyperlinkChanger( contact_Us )">Contact us</a>
    <br />

    <div name="content">
      <!--please help me write a method to call the output of the funcion hyperlinkChanger-->
      <iframe src="hyprlink function" width="600" height="300">
      </iframe>
    </div>
  </body>
</html>
最佳回答

Sans javascript:

<html>
<body>
    <iframe id="foo"></iframe>
    <a href="http://stackoverflow.com/questions/8240078/script-to-dynamically-change-the-source-of-an-iframe-tag" target="foo">This page</a>
</body>
</html>

You can target local or external links. Notice that in this example, javascript running on Stack Overflow detects its page is being loaded in an iframe and puts out a notice to the user. Some sites don t some do. There are even techniques to detect if your page is being loaded into an iframe and redirect the parent page to your site directly instead. If all you re doing is loading your own pages, though, you don t have to worry about this.

问题回答

First of all why do it with JAVASCRIPT? You can just use the target-attribute on the a-element!

But if you have to use javascript do something like this: declare var url = ; before the switch statement Remove the returns from you switch cases An after the switch statement add this: $( iframe ).attr( src , url);





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签