I want to familiarize myself with javascript and do not know jquery. As a leading javascript framework, many of its features are excited. What I have to mention is the unique concatenation writing syntax, so how can he achieve it? Let's also implement one.
Copy codeThe Code is as follows:
Sx. $ = function (id ){
Var t = (typeof (id) = "string "? Document. getElementById (id): id );
T. text = function (){
Return this. innerText? This. innerText: this. innerHTML. replace (// <.*? />/Igm ,"");
}
T.html = function (){
Return this. innerHTML? This. innerHTML: null;
}
T. first = function (){
Return this. firstChild? This. firstChild. nodeName! = "# Text "? Sx. $ (this. firstChild): null;
}
T. last = function (){
Return this. lastChild? This. lastChild. nodeName! = "# Text "? Sx. $ (this. lasChild): null;
}
T. pre = function (){
Return this. previussibling? Sx. $ (this. previussibling): null;
}
T. next = function (){
Return this. nextSibling? Sx. $ (this. nextSibling): null;
}
T. parent = function (){
Return this. parentNode? Sx. $ (this. parentNode): null;
}
T. setevent = function (e, f ){
If (t. attachEvent ){
T. attachEvent ("on" + e, f );
} Else {
T. addEventListener (e, f, false );
}
}
T. removeevent = function (e, f ){
If (t. dettachEvent ){
T. dettachEvent ("on" + e, f );
} Else {
T. removeEventListener (e, f, false );
}
}
T. setstyle = function (s ){
Var s = s. split (",");
For (var I = 0; I <s. length; I ++ ){
Var s1 = s [I]. split (":");
This. style [s1 [0] = s1 [1];
}
}
T. getstyle = function (s ){
If (this. currentStyle ){
Return this. currentStyle [s];
} Else {
Return document. defaultView. getComputedStyle (this, null). getPropertyValue (s );
}
}
T. selectpath = function (m ){
Var m1 = m;
Var m = m. split ("/");
Var t = [];
Var e = this. getElementsByTagName ("*");
For (var I = 0; I <e. length; I ++ ){
Var e1 = e [I]
Var a = "";
Var i1 = m. length-1;
While (e1! = This ){
A = e1.tagName + "/" +;
E1 = e1.parentNode;
// Alert ();
}
// Alert ();
If (m1.toLowerCase () + "/" = a. toLowerCase ()){
T. push (sx. $ (e [I]);
}
}
Return t;
}
T. get = function (){
Return this. getAttribute ();
}
T. set = function (a, v ){
Return this. setAttribute (a, v );
}
T. paste = function (h ){
If (typeof (h) = "string "){
Var d = document. createElement ("span ");
D. innerHTML = h;
} Else {
Var d = document. createElement ("span ");
D. appendChild (h );
}
Var t1 = this. childNodes;
For (var I = 0; I <t1.length; I ++ ){
Alert (t1 [I])
This. removeChild (t1 [I]);
}
This. appendChild (d );
D. removeNode (false );
}
Return t;
}
Sx. $ = function (){
Var t = []
For (var I = 0; I <arguments. length; I ++ ){
T. push (sx. $ (arguments [I])
}
Return t;
}
The above code is a core piece of code that I recently wrote in the cross-platform javascript framework. It can be seen that I use recursive concatenation syntax and constantly call myself in my own methods, in this way, the experiment closure enables continuous object operations. by the way, I use methods for the t object here. If it is an attribute, it will be displayed in innerHTML.
I'm about to take the test now. I don't have much time to study and study. Although my major has nothing to do with computer science, I wish I don't want to take a class.
I hope you can correct the shortcomings in this article.