Home > Article > Web Front-end > Summary of common and important jQuery methods_jquery
1.jquery data(name)
The data() method appends data to the selected element, or obtains data from the selected element.
$("#btn1").click(function(){ $("div").data("greeting", "Hello World"); }); $("#btn2").click(function(){ alert($("div").data("greeting")); });
2.jquery arguments
arguments is a reference to the actual parameter object, which is an array-like object.
The index of arguments increases from 0, 1,2,...., corresponding to the actual parameters one by one.
arguments.length attribute represents the number of actual parameters
arguments must not be an array, but an object that is long and looks like an array, although it also has a length attribute
Arguments will be found in every function. Therefore, arguments will only find their own arguments internally and cannot refer to outer arguments
// 求圆形面积,矩形面积, 三角形面积 function area () { if(arguments.length == 1) { alert(3.14 * arguments[0] * arguments[0]); } else if(arguments.length == 2) { alert(arguments[0] * arguments[1]); } else if(arguments.length == 3) { alert(arguments[0] + arguments[1] + arguments[2]); } else { return null; } } //调用 area(10,20,30);
3.jquery target() event.target
Thetarget attribute specifies which DOM element triggered the event.
$("p, button, h1, h2").click(function(event){ $("div").html("Triggered by a " + event.target.nodeName + " element."); }); <p></p> <button></button> <h1></h1> <h2></h2> //当点击p标签的时候显示:点击事件由 P 元素触发 ....
4.jquery trigger(event,[parameter 1, parameter 2,...])
Thetrigger() method triggers the specified event type of the selected element. (Events can be customized and parameters can be passed) Custom events are very important and useful!
//myEvent为自定义事件名 $("#p1").bind("myEvent",function(str1,str2) { alert(str1 + ' ' + str2); }); $("#p1").trigger("myEvent",["Hello","World"]); //也可以这样写: $("#p1").bind("myEvent",function(str1,str2) { alert(str1 + ' ' + str2); }).trigger("myEvent",["Hello","World"]);
5.js substring(start,stop)
Thesubstring() method is used to extract characters between two specified subscripts in a string.
The substring returned by thesubstring() method includes the characters at start but does not include the characters at stop.
var str="Helloworld!" document.write(str.substring(3,7)) //结果 lowo var str="Hello world!"//有两个空字符 document.write(str.substring(3,7)) //结果 lo //两者的结果有区别,字符串之间的空字符串占用索引!
Look clearly, there is no r character at the stop!
Important: Unlike the slice() and substr() methods, substring() does not accept negative arguments.
6.js slice(start,stop)
Theslice() method extracts a certain part of a string and returns the extracted part as a new string. Same as the substring above, excluding the characters at the stop;
Another difference is: start and stop can use negative numbers! That is, -1 refers to the last character of the string, -2 refers to the second to last character, and so on.
The data() method appends data to the selected element, or obtains data from the selected element.
The above is the entire content of this article, I hope you all like it.