问题描述:

this code works:

<div class="demo-gallery" data-pswp-uid="1">

<a class = "delete_button">click</a>

<script src="https://code.jquery.com/jquery-2.1.4.js"></script>

<script>

$(document).ready(function()

{

$(".delete_button").click( function (event) {

event.preventDefault();

alert('hi');

return false;

});

});

</script>

DEMO: http://jsfiddle.net/zqdpfvzo/

but not works if I put script under jquery-2.1.4.js:

DEMO: http://jsfiddle.net/zqdpfvzo/1/

why and how can Fixed it?

网友答案:

Because the scripts load in the order that they are declared. In the second demo, the in-page code attempts to run before jQuery has been defined and loaded, thus errors out and does nothing:

网友答案:

Just like the way you can not use the value of a variable before declaring and initializing it, the same way you need to declare and initialize the variable jQuery first, then only you can use it properties.

The scripts are being loaded in browser syncronously in the same order in which they are written.

Trying to do a analogy:

Issue is:

alert(x);
var x = 5;

Fix is:

var x = 5;
alert(x);
相关阅读:
Top