简体   繁体   中英

Changing Iframe's src with Jquery

I am trying to change iframe's src attribute with jQuery. But this code isnt working. Even the alert doesnt show up.

JS:

<script src="../Scripts/jquery-1.11.0.js" type="text/javascript"></script>
<script type="text/javascript">

 function loadIframe(url) {

  var $iframe = $('#' + <%=iPage.ClientID%>);//Also tried $('#<%=iPage.ClientID%>')
    if ( $iframe.length ) {
       $iframe.attr('src',url);   
       return false;
    }
    return true;
}
</script>

ASPX:

<li>
  <asp:LinkButton id="link1" runat="server" OnClientClick="loadIframe( 
   'www.asd1234.com')" Text="Test"></asp:LinkButton>
</li>
<asp:updatepanel...>
  //.....
  <iframe id="iPage" runat="server"></iframe>
</asp:updatepanel>

As you are using

<iframe id="iPage" runat="server"></iframe>

You need to use Control.ClientID

<asp:LinkButton 
    id="link1" 
    runat="server" 
    OnClientClick="loadIframe('<%= iPage.ClientID %>', 'www.google.com')" 
    Text="Test"></asp:LinkButton>

Also move function out of the document ready handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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