"The script won't work as expected if you access the file directly with a browser(when clicking to it,no alert).But if you loaded it to another page with $.load(),it will work(alert when you click). – Shore"
When you load the file in browser as itself, the reason why there's no alert when you click it is because when the browser reads the line "$('.close')...
", there's no DOM object of class "close
" yet, because the comes after the script tag.
You can resolve this by:
$(document).ready(function(){$('.close').click(function() { alert(1) });});
As for the delay part, it is because that when you use $.load(), the function loads the file first, then put the source into the DOM object. After putting source into the DOM object, it will then parse the <script>
tags in your file. That's why your click event is binded successfully to your DOM objects with class close