<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('確認(rèn)審核通過如贷?',{title:'頭像審核'}, function (){
}, function (){
});
});
$(document).on('click','.outer.default', function (){
$.weui.confirm('確認(rèn)不合格?',{title:'頭像審核'}, function (){
}, function (){
});
});
$('.search_bar_wrap').searchBar({
//替換原模板的“取消”
cancelText:"取消",
//替換原模板的“搜索”
searchText:'可搜索昵稱到踏、QQ號碼、學(xué)校',
//搜索欄獲得焦點時
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方法窝稿,則沿用瀏覽器默認(rèn)的提交方式
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了,發(fā)現(xiàn)自己已擼凿掂,依然連代碼都不會寫了伴榔,好在之前的思維都在。
js操作用得最多的庄萎,無非有以下幾點:
- 1踪少、操作dom節(jié)點,包括糠涛,查找援奢,動態(tài)添加dom
- 2、ajax發(fā)送網(wǎng)絡(luò)請求忍捡,要知道跨域如何處理集漾。
- 3、不能在多了砸脊,就以上兩點了具篇。
對于操作dom節(jié)點來說,其最主要的是要去定位你要找的dom節(jié)點凌埂!
然而我們已經(jīng)回不到那個findElementById
的那個時代了驱显。
就jquery來說吧,移動端zepto其實也是一樣瞳抓。
#
對應(yīng)于id
埃疫,.
對應(yīng)于class
相信懂的人一看就會,但是其他的挨下,你不經(jīng)常寫熔恢,未必就記得,不記得怎么辦臭笆,參考這里:
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 表達(dá)式來選擇帶有給定屬性的元素茂洒。
$("[href]") 選取所有帶有 href 屬性的元素孟岛。
$("[href='#']") 選取所有帶有 href 值等于 "#" 的元素。
$("[href!='#']") 選取所有帶有 href 值不等于 "#" 的元素督勺。
$("[href$='.jpg']") 選取所有 href 值以 ".jpg" 結(jié)尾的元素渠羞。
- 更多的選擇器實例
$(this) 當(dāng)前 HTML 元素
$("p") 所有 <p> 元素
$("p.intro") 所有 class="intro" 的 <p> 元素
$(".intro") 所有 class="intro" 的元素
$("#intro") id="intro" 的元素
$("ul li:first") 每個 <ul> 的第一個 <li> 元素
$("[href$='.jpg']") 所有帶有以 ".jpg" 結(jié)尾的屬性值的 href 屬性
$("div#intro .head") id="intro" 的 <div> 元素中的所有 class="head" 的元素
$("a,li,p") 所有的,a智哀,li次询,p元素。
其次想回顧下的主要有兩個方面瓷叫,事件屯吊,以及操作文檔。
對于事件摹菠,也不想作太多的回顧盒卸,用得最多的無非就是click,但是有一點需要注意次氨,動態(tài)添加的文本蔽介,也想有click事件怎么辦?
以下兩種糟需,均不會對后續(xù)動態(tài)增加進(jìn)來的元素產(chǎn)
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){ ... })
而以下兩種均會對后續(xù)動態(tài)添加進(jìn)來的a,節(jié)點屉佳,nav 下的 a節(jié)點其作用。
// 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)
最后洲押,想回顧的自然是網(wǎng)絡(luò)相關(guān)的操作武花,當(dāng)然,本人也很懶杈帐,只想回顧下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'
})