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;">     &lt;script type="text/javascript"&gt;         $("a").on("click", function(e) {   alert(1); });     &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</iframe></body></html> 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -