jsweb常用代码
<script>
$(function (){
$.ajax({
url: 'https://test.com:8080/api/v1/users?query_not_auth=100&start=0&num=10',
dataType: 'jsonp',
headers:{
'x-user':'1111',
'Authorization':'Bearer '+'111111'
},
success:function(data){
if (data != null && data.users != null) {
for (var i = 0; i < data.users.length; i++) {
$('.search_bar_wrap').after(generateDom(data.users[i]));
}
}
}
});
$(document).on('click','.outer.primary', function (){
$.weui.confirm('确认审核通过?',{title:'头像审核'}, function (){
}, function (){
});
});
$(document).on('click','.outer.default', function (){
$.weui.confirm('确认不合格?',{title:'头像审核'}, function (){
}, function (){
});
});
$('.search_bar_wrap').searchBar({
//替换原模板的“取消”
cancelText:"取消",
//替换原模板的“搜索”
searchText:'可搜索昵称、QQ号码、学校',
//搜索栏获得焦点时
onfocus: function (value) {
console.log('focus!The value is '+value);
},
//搜索栏失去焦点时
onblur:function(value) {
console.log('blur!The value is '+value);
},
//搜索栏在输入时
oninput: function(value) {
console.log('Input!The value is '+ value);
},
//搜索栏提交时,如果没有submit方法,则沿用浏览器默认的提交方式
onsubmit:function(value){
console.log('Submit!The value is '+ value);
},
//点击取消按钮
oncancel:function(){
console.log('click cancel');
},
//点击清空按钮
onclear:function(){
console.log('click clear');
}
});
})
function generateDom(user){
var type1 = `
<div class="weui_panel weui_panel_access panel">
<div class="weui_panel_hd apply-id">$time $name <span>申请</span><span>$gender</span></div>
<div class="weui_panel_bd">
<div class="weui_media_box weui_media_text">
[站外图片上传中……(1)]
</div>
</div>
<div class="weui_dialog_ft panel-btn">
<a href="javascript:;" class="weui_btn_dialog outer primary">审核通过</a>
<a href="javascript:;" class="weui_btn_dialog outer default">不合格</a>
</div>
</div>`;
var type2 = `
<div class="weui_panel weui_panel_access panel">
<div class="weui_panel_hd apply-id">$time $name <span>申请</span><span>$gender</span></div>
<div class="weui_panel_bd">
<div class="weui_media_box weui_media_text">
[站外图片上传中……(2)]
</div>
</div>
<div class="weui_panel_hd panel-bottom">$state</div>
</div>`;
var result = '';
switch(user.jobauth){
case 0:
case 100:
result = type1.replace('$time',timestamp2date(user.updatetime)).replace('$name',user.name).replace('$gender',user.gender==1?'男':'女').replace('$pic',user.head);
break;
case 400:
case -400:
result = type2.replace('$time',timestamp2date(user.updatetime)).replace('$name',user.name).replace('$gender',user.gender==1?'男':'女').replace('$pic',user.head).replace('$state',user.jobauth==400?"已审核通过":"已拒绝");
break;
}
return result;
}
function timestamp2date(timestamp){
var date = new Date();
date.setTime(timestamp);
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ' ';
return M+D+h+m;
}
</script>
很久不撸js了,发现自己已撸,依然连代码都不会写了,好在之前的思维都在。
js操作用得最多的,无非有以下几点:
- 1、操作dom节点,包括,查找,动态添加dom
- 2、ajax发送网络请求,要知道跨域如何处理。
- 3、不能在多了,就以上两点了。
对于操作dom节点来说,其最主要的是要去定位你要找的dom节点!
然而我们已经回不到那个findElementById的那个时代了。
就jquery来说吧,移动端zepto其实也是一样。#对应于id,.对应于class相信懂的人一看就会,但是其他的,你不经常写,未必就记得,不记得怎么办,参考这里:
http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp 。
如果你很懒,那么我也不得不贴一些要点出来:
- jQuery 元素选择器 | jQuery 使用 CSS 选择器来选取 HTML 元素。
$("p") 选取 <p> 元素。
$("p.intro") 选取所有 class="intro" 的 <p> 元素。
$("p#demo") 选取所有 id="demo" 的 <p> 元素。- jQuery 属性选择器 | jQuery 使用 XPath 表达式来选择带有给定属性的元素。
$("[href]") 选取所有带有 href 属性的元素。
$("[href='#']") 选取所有带有 href 值等于 "#" 的元素。
$("[href!='#']") 选取所有带有 href 值不等于 "#" 的元素。
$("[href$='.jpg']") 选取所有 href 值以 ".jpg" 结尾的元素。- 更多的选择器实例
$(this) 当前 HTML 元素
$("p") 所有 <p> 元素
$("p.intro") 所有 class="intro" 的 <p> 元素
$(".intro") 所有 class="intro" 的元素
$("#intro") id="intro" 的元素
$("ul li:first") 每个 <ul> 的第一个 <li> 元素
$("[href$='.jpg']") 所有带有以 ".jpg" 结尾的属性值的 href 属性
$("div#intro .head") id="intro" 的 <div> 元素中的所有 class="head" 的元素
$("a,li,p") 所有的,a,li,p元素。
其次想回顾下的主要有两个方面,事件,以及操作文档。
对于事件,也不想作太多的回顾,用得最多的无非就是click,但是有一点需要注意,动态添加的文本,也想有click事件怎么办?
以下两种,均不会对后续动态增加进来的元素产
on(type, [selector], function(e){ ... }) ⇒ self
on(type, [selector], [data], function(e){ ... }) ⇒ self
on({ type: handler, type2: handler2, ... }, [selector]) ⇒ self
on({ type: handler, type2: handler2, ... }, [selector], [data]) ⇒ self
var elem = $('.content')
// observe all clicks inside dom of class named content:
elem.on('click', function(e){ ... })
// observe clicks inside navigation links in .content
elem.on('click', 'nav a', function(e){ ... })
而以下两种均会对后续动态添加进来的a,节点,nav 下的 a节点其作用。
// all clicks inside links in the document
$(document).on('click', 'a', function(e){ ... })
// disable following any navigation link on the page
$(document).on('click', 'nav a', false)
最后,想回顾的自然是网络相关的操作,当然,本人也很懒,只想回顾下ajax罢了:
- type
(default: “GET”): HTTP request method (“GET”, “POST”, or other)- url
(default: current URL): URL to which the request is made- data
(default: none): data for the request; for GET requests it is appended to query string of the URL. Non-string objects will get serialized with $.param- processData
(default: true): whether to automatically serialize data
for non-GET requests to string- contentType
(default: “application/x-www-form-urlencoded”): the Content-Type of the data being posted to the server (this can also be set via headers
). Pass false
to skip setting the default value.- mimeType
(default: none): override the MIME type of the response. v1.1+- dataType
(default: none): response type to expect from the server. One of json
, jsonp
, script
, xml
,html
, or text
.- jsonp
(default: “callback”): the name of the JSONP callback query parameter- jsonpCallback
(default: “jsonp{N}”): the string (or a function that returns) name of the global JSONP callback function. Set this to enable browser caching. v1.1+- timeout
(default: 0
): request timeout in milliseconds, 0
for no timeout- headers
: object of additional HTTP headers for the Ajax request- async
(default: true): set to false
to issue a synchronous (blocking) request- global
(default: true): trigger global Ajax events on this request- context
(default: window): context to execute callbacks in- traditional
(default: false): activate traditional (shallow) serialization of data
parameters with $.param- cache
(default: true): whether the browser should be allowed to cache GET responses. Since v1.1.4, the default is false
for dataType: "script"
or jsonp
.- xhrFields
(default: none): an object containing properties to be copied over verbatim to the XMLHttpRequest instance. v1.1+- username & password
(default: none): HTTP Basic authentication credentials. v1.1+
$(document).on('ajaxBeforeSend', function(e, xhr, options){
// This gets fired for every Ajax request performed on the page.
// The xhr object and $.ajax() options are available for editing.
// Return false to cancel this request.
})
$.ajax({
type: 'GET', url: '/projects',
// data to be added to query string:
data: { name: 'Zepto.js' },
// type of data we are expecting in return:
dataType: 'json',
timeout: 300,
context: $('body'),
success: function(data){
// Supposing this JSON payload was received:
// {"project": {"id": 42, "html": "<div>..." }}
// append the HTML to context object.
this.append(data.project.html)
},
error: function(xhr, type){ alert('Ajax error!') }
})
// post a JSON payload:
$.ajax({
type: 'POST',
url: '/projects',
// post payload:
data: JSON.stringify({ name: 'Zepto.js' }),
contentType: 'application/json'
})
jsweb常用代码的更多相关文章
- GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- 转--Android实用的代码片段 常用代码总结
这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下 1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...
- 刀哥多线程之03GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- jquery常用代码集锦
1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({ ajaxSettings : { contentT ...
- Mysql:常用代码
C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...
- javascript常用代码大全
http://caibaojian.com/288.html 原文链接 jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_ca ...
- Android 常用代码大集合 [转]
[Android]调用字符串资源的几种方法 字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...
- NSIS常用代码整理
原文 NSIS常用代码整理 这是一些常用的NSIS代码,少轻狂特意整理出来,方便大家随时查看使用.不定期更新哦~~~ 1 ;获取操作系统盘符 2 ReadEnvStr $R0 SYSTEMDRIVE ...
- PHP常用代码大全(新手入门必备)
PHP常用代码大全(新手入门必备),都是一些开发中常用的基础.需要的朋友可以参考下. 1.连接MYSQL数据库代码 <?php $connec=mysql_connect("loc ...
随机推荐
- c++——对象的动态建立和释放(new 和delete)
3.8 对象的动态建立和释放 1 new和delete基本语法 1)在软件开发过程中,常常需要动态地分配和撤销内存空间,例如对动态链表中结点的插入与删除.在C语言中是利用库函数malloc和free来 ...
- 针对Windows 64位系统中Matlab没有LED Control Activex控件的解决方法
Win 10 64bits系统中Matlab 64位软件没有LED Control Activex控件,LED ActiveX Control控件位于Gauges Blockset模块中,而Gauge ...
- 20145234黄斐《java程序设计》第十三周代码检查
在IDEA中对P145 MathTool.java 使用JUnit进行单元测试,测试用例不少于三个,要包含正常情况,边界情况. 提交测试代码和运行结果截图,加上学号水印,提交码云代码链接. 码云链接 ...
- 使用cgroups来控制内存使用
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackga ...
- [Qt扒手2] PyQt5 路径绘画例子
[说明] 此例扒自 Qt 官网,原例是 C++ 代码,我把它改写成了 Python + PyQt5 版本. 有了前一个例子的成功,这个例子改写的非常之快.记得第一个例子花了我几天的时间,而这个例子只花 ...
- [ATL/WTL]_[初级]_[关于graphics.DrawImage绘图时显示不正常的问题]
场景 1.使用win32绘图时, 最简单的api是使用 graphics.DrawImage(image,x,y)来绘制, 可是这个api有个坑,它的图片显示完整和设备分辨率(显卡)有关. 说明 1. ...
- Python_sklearn机器学习库学习笔记(六) dimensionality-reduction-with-pca
# 用PCA降维 #计算协方差矩阵 import numpy as np X=[[2,0,-1.4], [2.2,0.2,-1.5], [2.4,0.1,-1], [1.9,0,-1.2]] np.c ...
- pdo的用处,用法
PDO主要是用来对数据库进行访问的.PDO扩展为PHP访问数据库定义了一个轻量级的一致接口,不同数据库在访问时,采用相同方法名称,解决了连接数据库不统一问题.PDO扩展自身并不能实现任何数据库功能,必 ...
- 15-RUN vs CMD vs ENTRYPOINT
RUN.CMD 和 ENTRYPOINT 这三个 Dockerfile 指令看上去很类似,很容易混淆.本节将通过实践详细讨论它们的区别. 简单的说: RUN 执行命令并创建新的镜像层,RUN 经常用于 ...
- Windows下Redis集群搭建
1.第一步先安装Redis 参照<Windows下Redis安装及使用.docx> 在Redis目录E:/Redis下新建Logs文件夹,并且创建3个端口下的配置文件,记得修改里面的接口 ...