<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css"/>
</head>
<body>
<script>
// 匿名函数;
setInterval(function(){console.log(10);},2000);
// 自执行函数,应用场景:调用别人开发好的js,可能不同厂商的js函数的函数名相同,那就不好了,所以写自执行函数一次性执行完,也不用管函数名相同了
(function(arg){console.log(arg);})(30)
// 就相当于
function func(arg){
console.log(arg);
}
func(40);
</script>
</body>
</html>
封装函数
function add_fav(cur_ele,fav_id,fav_type){
$.ajaxSetup({
beforeSend:function(xhr,settings){
xhr.setRequestHeader('X-CSRFtoken',"{{ csrf_token }}");
}
});
$.ajax({
url:'{% url 'course:add_fav' %}',
type:'POST',
data:{'fav_id':fav_id,'fav_type':fav_type},
success:function(data){
var ret = JSON.parse(data);
if(ret['status'])
{cur_ele.html(ret['data'])}
else{
window.location.href="/login/";
}
},
error:function(){
alert('error')
}
})
}
$("#jsLeftBtn").on('click',function(){
add_fav($(this),{{ course.id }},1);
});
$("#jsRightBtn").on('click',function(){
console.log($(this));
add_fav($(this),{{ course.id }},1);
});
页面重定向
window.location.href="/login/";
页面刷新
window.location.reload();