Trying to add iframe using jQuery

I am trying to add an iframe using jquery below, but when I click the link, nothing happens.

<head> <script type="text/javascript" src='//ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js'></script> <script type="text/javascript"> function fun(){ $('<iframe />'); // Create an iframe element $('<iframe />', { name: 'frame1', id: 'frame1', src: 'http://www.programmingfacts.com' }).appendTo('body'); </script> </head> <body> <a href="#" onclick="fun()">clica</a> </body> </html> 
+4
source share
3 answers

I assume that you are not closing your fun () function of the closing parenthesis? }

+7
source

Try the following:

 <html> <head> <script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js'></script> <script type="text/javascript"> $(document).ready(function() { $(".submit").click(function() { $('<iframe />'); // Create an iframe element $('<iframe />', { name: 'frame1', id: 'frame1', src: 'http://www.programmingfacts.com' }).appendTo('body'); }); }); </script> </head> <body> <a href="#" class="submit">clica</a> </body> </html> 
+7
source

Change your function to:

 function letsHaveFun(){ $('body').append('<iframe src="http://www.programmingfacts.com" name="frame1" id="frame1"></iframe>'); } 

That should work.

+2
source

Source: https://habr.com/ru/post/1338075/


All Articles