jQuery notes

-->resource


basic use of jQuery:

$(selector).action()

What to notice is that $('p') selects all 'p' tags.We can use $('p.test') to select all 'p' tags with 'class="test" '. Use id to select is OK as well.

$(document).ready(function () { ... })

This will prevent jQuery to run before document is ready .And it's same as :

$( function (){...} )


selector

-If u want to find elements with certain attributes , for example, select every elements with ' href="..." ', u can :

$("[ href ]")

-If some 'input' tags with ' type = " text " ' ,and u want 2 select them :

$("input [type="text"]")

-If want to select elements with same type (e.g. type = "button" ):

$(" :button ")


action

-click()
-dbclick() : double click
-mouseenter() : when mouse entered what u selected. For example : when slice mouse to '<p> hide this </p> ' then hide:

$("p").mouseenter(function (){
$(this).hide();
});

-hover() :When enter , run first function , and when leave, run second function :

$("p").hover (
function () {
alert (' you enter <p>')
},
function () {
alert (' you left <p>')
}
);

-focus() : When click or select elements through 'Tab', this element is focused and we can change its style or run function.

hide && show && toggle

$(selector).hide(speed,callback);
$(selector).show(speed,callback);
$(selector).toggle(speed,callback);

-speed : 'slow' or 'fast' or how many ms ;
-callback : function here will run immediately , instead of running after 'hide' or 'show' or 'toggle' .Else , if u selected several elements , callback will run several times.
-Toggle means 'switch' here . When it's hidden , you can use 'toggle ' to show it ,and when shown you can use 'toggle' to hide it.

fading

-fadeIn() : fade in what is hidden
-fadeOut() : fade out what can be seen
-fadeToggle() : fadeIn()+fadeOut()
-fadeTo() : can change opacity (between 0~1)

$(selector).fadeTo(speed,opacity,callback);


slide

$(selector).slideDown(speed,callback);
$(selector).slideUp(speed,callback);
$(selector).slideToggle(speed,callback);


chaining

$("#p1").css("color","red").slideUp(2000).slideDown(2000);


jQuery HTML

$("#selectById").text()
$("#selectById").html()
$("#selectById").val()

-text() : get text in selected element
-html() : get html in selected element
-val() : get what user inputs in selected element


-attr() : get or set attribute of the selected element ,for example ,we select img tag's attribute, 'src' :

$("button").click(function(){
  alert( $("#selectById").attr("src") );
});

basic use :

$("selector").attr(  "attribure" : " ... " );
$("selector").attr(  "attribure" , function () { ... } );
$("selector").attr({"attribure1" : " ... " ,
"attribure2" : "..."  });

function : what the function returns will change the attribute you want to set by ' attr() ' method.

jQuery HTML -- add elements && delete elements && css()

-append() : insert info in the end of selected element
-prepend() : insert at the beginning
-after() : insert info after selected element
-before()
All of these four can add new elements to html , for e.g. ,add <tr></tr> to table:

function addtotable(){
let newtr = ` <tr><td> new row to table</td></tr>`;
$("#mytable").append(newtr);
}



-remove() : delete selected elements (& what belongs to them )
-empty() : delete what belongs to it

$("table").remove(".table-hover");



jQuety css()

$("selector")css("propertyname","value");
$("selector")css({"propertyname":"value","propertyname":"value",...});


jQuery - AJAX

  • load() : load data from server , and put the data into selected element.
$("selector").load(URL,data,callback);

callback parameter contains:
-responseTxt : show contains when success
-statusTxt : status
-xhr : XMLHttpRequest object
e.g.

$("button").click(function(){
 $("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
    if(statusTxt=="success")
      alert("外部內(nèi)容加載成功!");
    if(statusTxt=="error")
      alert("Error: "+xhr.status+": "+xhr.statusText);
  });
});
  • get()
$.get(URL,function (data, status));
  • post()
$.post(URL,data,callback);

'Data' will be sent as request , and we can use what the server responses in callback .




Thank u for reading ~XD

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末卵惦,一起剝皮案震驚了整個濱河市记罚,隨后出現(xiàn)的幾起案子盗痒,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蜂厅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來膊畴,“玉大人掘猿,你說我怎么就攤上這事〈娇纾” “怎么了稠通?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長买猖。 經(jīng)常有香客問我采记,道長,這世上最難降的妖魔是什么政勃? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任唧龄,我火速辦了婚禮,結(jié)果婚禮上奸远,老公的妹妹穿的比我還像新娘既棺。我一直安慰自己,他們只是感情好懒叛,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布丸冕。 她就那樣靜靜地躺著,像睡著了一般薛窥。 火紅的嫁衣襯著肌膚如雪胖烛。 梳的紋絲不亂的頭發(fā)上眼姐,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機(jī)與錄音佩番,去河邊找鬼众旗。 笑死,一個胖子當(dāng)著我的面吹牛趟畏,可吹牛的內(nèi)容都是我干的贡歧。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼赋秀,長吁一口氣:“原來是場噩夢啊……” “哼利朵!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起猎莲,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤绍弟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后著洼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體樟遣,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年郭脂,在試婚紗的時候發(fā)現(xiàn)自己被綠了年碘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片澈歉。...
    茶點(diǎn)故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡展鸡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出埃难,到底是詐尸還是另有隱情莹弊,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布涡尘,位于F島的核電站忍弛,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏考抄。R本人自食惡果不足惜细疚,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望川梅。 院中可真熱鬧疯兼,春花似錦、人聲如沸贫途。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽丢早。三九已至姨裸,卻和暖如春秧倾,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背傀缩。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工那先, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人扑毡。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓胃榕,卻偏偏與公主長得像,于是被迫代替她去往敵國和親瞄摊。 傳聞我的和親對象是個殘疾皇子勋又,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容