javascript - Why the jQuery snippet works without the IFrame, but does not with an IFrame? -
works
$("a").on("click", function(e) { alert(1); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <a href="#">link</a> </div> does not work
$("a").on("click", function(e) { alert(1); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <a href="#">link</a> </div> <iframe id="myiframe" src="" style="width: 200px; height: 200px;" />
because markup invalid. iframe not void element. should close iframe properly.
the first rendered html in chrome browser:
<html><head> <style> </style> </head> <body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <a href="#">link</a> </div> <script type="text/javascript"> $("a").on("click", function(e) { alert(1); }); </script> </body></html> the second one:
<html><head> <style> </style> </head> <body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <a href="#">link</a> </div> <iframe id="myiframe" src="" style="width: 200px; height: 200px;"> <script type="text/javascript"> $("a").on("click", function(e) { alert(1); }); </script> </body> </html></iframe></body></html>
Comments
Post a Comment