Javascript – différentes manières d’exécuter du JS
js pur et dur
<!-- js pur et dur-->
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
document.getElementById("monbouton").onclick=function(){
document.getElementById("demo").innerHTML="toto";
}
}
</script>
</head>
<body>
<p>Click the button to trigger a function.</p>
<button onclick="myFunction()">Click me 1</button>
<button id="monbouton">Click me 2</button>
<p id="demo"></p>
</body>
</html>
jquery + onload
<!-- jquery + onload -->
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(window).load(function(){
$("#monbouton").onclick=function(){
document.getElementById("demo").innerHTML="toto";
}
});
</script>
</head>
<body>
<p>Click the button to trigger a function.</p>
<button id="monbouton">Click me 2</button>
<p id="demo"></p>
</body>
</html>
jquery à la fin du code
<!-- jquery à la fin du code--->
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.9.1.min.js"></script>
</head>
<body>
<p>Click the button to trigger a function.</p>
<button class="test">Click me 2</button>
<p class="demo"></p>
</body>
<script>
$('.test').click(function(){
$(".demo").html("toto");
alert('toto');
});
</script>
</html>