jQuery补充及Django基本使用_Day18

 jQuery扩展

onclick

1.  onclick属性的正确写法是onclick="函数名()",它是带括号的,而不是onclick="函数名"

2.如果onclick属性没有传入this对象,则在函数定义中不能使用$(this),否则解释器会因为找不到该对象而停止运行。

绑定事件

在标签上绑定

找到标签绑定

1  $('.title').click(function(){

var v = $(this).text();

console.log(v)

})

2  $('.title').bind('click',function(){

var v = $(this).text();

console.log(v)

})

上面两种完全一致

3.委托

$('.title').delegate('.title','click',function(){

var v = $(this).text();

console.log(v)

})

4. $('.c1').on('.title','click',function()){

var v = $(this).text();

console.log(v)

}

四种结果一样

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <input type="text" id="inp"><input id="btn" type="submit" value="添加" />
 9     <ul>
10         <li>内容一</li>
11         <li>内容二</li>
12     </ul>
13     <script src="jquery-1.12.4.js"></script>
14     <script>
15 
16 
17         $(function () {
18             $('#btn').click(function () {
19             var v = $('#inp').val();
20             var li = document.createElement('li');
21             li.innerHTML = v;
22             $('ul').append(li);
23         });
24 
25             $('ul').on('click','li',function () {
26             var v = $(this).text();
27             v = v + '+1';
28             $(this).text(v);
29         })
30         })
31 
32     </script>
33 </body>
34 </html>

 Ajax

不刷新页面,偷偷的向后台发送请求,不能return redirector

格式如下:

$.ajax({
url: '/aj/', # 提交地址
type: "POST", # 提交方式
data: {uuu: u, ppp:p}, # 提交数据
dataType: "JSON",
success:function (data) { # 回调函数,登录成功后自动执行
# 将字典形式的字符串,发序列化成为字典对象(json对象)
# var data_dict = JSON.parse(data);

if(data_dict.status){
location.href = "/home/"
}else{
alert(data_dict.error);
}
}
})

Django安装和使用

安装

python pip3.5 install django

创建project和app

创建project
先进入自己指定的目录
django-admin startproject mysite

mysite
- mysite (配置文件)
-urls.py 配置url对应关系
- manage.py (管理Project)
- app(cmdb)
- models.py 数据库操作
- admin.py 配置Django自带的后台管理
- apps.py 当前app的配置
- tests.py 单元测试
- views.py 做业务处理...

原文地址:https://www.cnblogs.com/liumj0305/p/6517141.html