代码之家  ›  专栏  ›  技术社区  ›  shinjuo

用javascript更改iframe-src

  •  71
  • shinjuo  · 技术社区  · 14 年前

    当有人单击单选按钮时,我正在尝试更改iframe-src。由于某种原因,我的代码不能正常工作,我很难找出原因。以下是我的资料:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
      <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
      <title>Untitled 1</title>
    
      <script>
      function go(loc) {
        document.getElementById('calendar').src = loc;
      }
      </script>
    </head>
    
    <body>
      <iframe id="calendar" src="about:blank" width="1000" height="450" frameborder="0" scrolling="no"></iframe>
    
      <form method="post">
        <input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Day
        <input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Week
        <input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Month
      </form>
    
    </body>
    
    </html>
    7 回复  |  直到 14 年前
        1
  •  105
  •   awendt    6 年前

    在这种情况下,可能是因为您使用了错误的括号:

    document.getElementById['calendar'].src = loc;
    

    应该是

    document.getElementById('calendar').src = loc;
    
        2
  •  54
  •   wizzwizz4    9 年前

    也许这会有帮助…它是纯HTML-没有javascript:

    <p>Click on link bellow to change iframe content:</p>
    <a href="http://www.bing.com" target="search_iframe">Bing</a> -
    <a href="http://en.wikipedia.org" target="search_iframe">Wikipedia</a> -
    <a href="http://google.com" target="search_iframe">Google</a> (not allowed in inframe)
    
    <iframe src="http://en.wikipedia.org" width="100%" height="100%" name="search_iframe"></iframe>

    顺便说一下,有些网站不允许您在iframe中打开它们(安全原因-点击劫持)

        3
  •  15
  •   Jim Clouse    10 年前

    下面是jquery的方法:

    $('#calendar').attr('src', loc);
    
        4
  •  6
  •   wizzwizz4    9 年前

    这个 onselect 必须是 onclick . 这对键盘用户有效。

    我还建议您增加 <label> 标记到“日”、“月”和“年”文本,使它们更易于单击。示例代码:

    <input id="day" name="calendarSelection" type="radio" onclick="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')"/><label for="day">Day</label>
    

    我还建议删除属性之间的空格 一次点击 以及值,尽管可以由浏览器解析:

    <input name="calendarSelection" type="radio" onclick = "go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')"/>Day
    

    应该是:

    <input name="calendarSelection" type="radio" onclick="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')"/>Day
    
        5
  •  5
  •   Vikrant Mayank    8 年前

    这也应该有效,尽管 src 将保持完整:

    document.getElementById("myIFrame").contentWindow.document.location.href="http://myLink.com";
    
        6
  •  0
  •   KBH    6 年前

    这是我的更新代码。它工作得很好,对你有帮助。

    <head>
      <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
      <title>Untitled 1</title>
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
      <script type="text/javascript">
      function go(loc) {
        document.getElementById('calendar').src = loc;
      }
      </script>
    </head>
    
    <body>
      <iframe id="calendar" src="about:blank" width="1000" height="450" frameborder="0" scrolling="no"></iframe>
    
      <form method="post">
        <input name="calendarSelection" type="radio" onclick="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Day
        <input name="calendarSelection" type="radio" onclick="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Week
        <input name="calendarSelection" type="radio" onclick="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Month
      </form>
    
    </body>
    
    </html>
    
        7
  •  -1
  •   Liam Joshua    7 年前

    您可以通过在javascript中生成iframe来解决这个问题。

      document.write(" <iframe  id='frame' name='frame' src='" + srcstring + "' width='600'  height='315'   allowfullscreen></iframe>");