js代码写在body里行,写在head里不行,为什么?有什么方法可以让js写在head里

<style>
.demo{
cursor:pointer;
}
</style>

</head>
<body>
<p class="demo">点击我</p>
<script>
$(".demo").click(function(){
alert("jQuery demo!");
});
</script>

$(document).ready(function () {
$(".demo").click(function(){
alert("jQuery demo!");
});
});
这样写就可以写在head里面了
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答