The next thing we want to talk about is how to write code to make our events simple. Ignore the technical events for now. If there is such a JS class now, there are several methods in it. These methods are enough for us to complete the above and more complex events than the above. This hypothetically existing class only has a method name and no specific implementation, so we call it an interface. What we are going to talk about next is defining the interface. When we feel that the methods in the interface can meet the needs, we will implement it. (This is a better development process, plan first and then act)
Instantiate an object
In the development project, there are more than just infinite-level node operation plug-ins, including plug-ins commonly used in b2c and b2b, such as rotation, paging, form validation, and form element retrieval. Setting values, uploading images in batches, repackaging events, batch processing of events, auto-completion of searches, ajax operations on shopping carts, and classes with public methods. Therefore, there will be many classes in the entire js, and each class corresponds to a js variable. In this way, there will be many public js variables. If a variable with the same name is accidentally declared elsewhere during development, then this class will be disappeared. In order to avoid this situation, I encapsulated all classes into a class named $jkit, so that there is only one public variable. The plug-in class has become local, how do I access it? I define another $CL class, which defines some methods. These methods are used to access the $jkit class. In this way, no matter how many plug-ins there are, there will only be two public variables, $CL and $jkit. $CL is responsible for calling $jkit. For example, newObj in $CL is used to instantiate the method of the plug-in object. newObj has two parameters. The first one specifies which plug-in is to be instantiated. The second parameter is used to initialize the plug-in when instantiating it. The parameters are passed in the form of an array.
Member methods of the new tree, When reading the following API, please keep two points in mind. First, all methods of the root object and descendant objects are for the original DOM object, such as calling del on the new object. , its essence is to delete the corresponding original DOM object; secondly, every time the addition, deletion or modification of the object is called, the corresponding branch will be reconstructed
Root object unique method
function map(index1,index2,,,indexN){} This method is used to find descendant nodes. table.map(1,1,0) will find the second row of The first object of the two cells is the object corresponding to select. When map has only one parameter, and the parameter is a DOM native object, then this method returns the corresponding new object.
function index(DOMElement){} This method returns the index corresponding to the native DOMElement object, table.index(document.getElementById('lior')), then returns [1,2 ,0,0], the result is in array form
Methods unique to descendant objects
function add(index, html){} This method is used to add sibling nodes. Index is the displacement relative to the position of the object that calls this method. HTML is what is to be inserted. Node, html can be any html string that conforms to W3c standards table.map(2).add(-1,'
< /td>
'), add a new line before the third line (multiple lines can be inserted at the same time) table.map(2).add(-2, '
'), add a new line before the previous line of the third line table.map(0).add(2,'
table.map(1).add(0,'
'), an index of 0 means adding a new row before the current row and deleting the current row table.map(1).add('< tr>
'), omitting the first parameter, this is a special usage, Regardless of which object is used, a new row will be added to the last row table.map(1,1).add(1,'
New cell
'), add a new cell after the second cell in the second row, and so on for further nodes, just use the map method to determine the node
function del(index){} This method is used to delete sibling nodes, index is the displacement relative to the position of the object that calls this method table.map(1) .del(), omitting index means deleting itself. Deleting the second row here is equivalent to table.map(1).del(0) table.map(0).del(2). Deleting here is relative to the current call. The second line after the object, here is to delete the third line table.map(2).del(-2), here is to delete the second line in front of the current calling object, here is to delete the first line table .map(0,1).del([0,-1,1]), if index is an array, then the sibling node of the specified index is deleted. At this time, it does not matter which object is called. A negative index means starting from the last Counting, -1 means the last one, here delete the first, second and last th table.map(0,1).del(0,-1), if there are two parameters, it means delete Specify the sibling nodes of the interval. At this time, it does not matter which object is called. A negative index means counting from the end, -1 means the last one. Here, delete the first to last elements. The larger parameter can be used as the first parameter. There is no limit to the order of size
function getParent(){} Get the native DOM object node corresponding to the parent object of the calling object, table.map(0,1).getParent().tagName is tr
function getHigher(){} Get the parent object of the calling object, table.map(0,1).getHigher.getNode().tagName is tr
Methods owned by root objects and descendant objects
function getNode(){} Get the native DOM object node corresponding to the calling object, table.getNode().tagName is table, table.map(0,1).getNode() for th
function sizeOf(){} Get the number of sub-objects of the calling object. table.sizeOf() is 3, which means there are three rows
function pos(){} Get the position of the calling object in all its sibling nodes, table.map(1).pos() is 1
function html(html){} Get the innerHTML of the native DOM object corresponding to the calling object. If there are parameters passed, assign a value to its innerHTML attribute (please do not assign a value to the read-only object innerHTML )
function attr(html){} Get the innerHTML of the native DOM object corresponding to the calling object. If there are parameters passed, assign values to their corresponding attributes (not yet implemented)
function before(index,html){} Add a node in front of the specified sub-object of the calling object, index is the relative displacement table.before(1,'
'), add a line table.map(1 ,2,0).before(-1,'
New li node
'), add a new li before the last li (the index is a negative number means counting from the end, -1 means the last) table.before('
'), omitting the first parameter means adding a new node in front of the first sub-object.
function append(index,html){} Add a node behind the specified sub-object of the calling object, index is the relative displacement table.append(1,'
'), add a line after the second line table.map(1 ,2,0).append(-1,'
New li node
'), add a new li after the last li (the index is a negative number means counting from the end, -1 means the last) table.append('
'), omitting the first parameter means adding a new node after the first sub-object.
function replace(index,html){} Replace the native DOM node corresponding to the specified sub-object of the calling object with the node generated by html. Index is the relative displacement table.replace( 2,'
New line
'), add a new line and use It replaces the second line table.replace(-1,'
New line
'), add a new line and replace the last line with it (a negative index means counting from the end, -1 means the last one)
function clean(index){} This method is used to delete sibling nodes. Index is the displacement relative to the position of the object that calls this method. table.clean(), Omitting index means deleting the first sub-object. Deleting the first row here is equivalent to table.map(1).del(0) table.clean(2). Here it means deleting the third row table.clean (-2), here means deleting the last row table.map(0).clean([0,-1,1]), if index is an array, then the sub-object of the specified index is deleted, and the index is a negative number means counting from the end, -1 means the last one, here delete the first, second and last th table.map(0).clean(0,-1), if there are two parameters, it means Delete the sub-objects in the specified range. A negative index means counting from the end, -1 means the last one. Here, delete the first to last elements. The larger parameter can be used as the first parameter. There is no limit on the size order
If the root node of the new tree is table, and its child nodes are tbody/thead/tfoot, since we will not operate these nodes more often, but directly operate tr, so I made a slightly allowed through the processing of these nodes. Of course, if you want to operate the tbody, you can pass the parameters like this ['tbody thead tfoot', 'tr', 'td']; if you only want to get one of them, you can ['tbody', 'tr', 'td ']; if you directly take tr, you can ['tr','td']. In this case, new objects will be generated for all tr in tbody/thead/tfoot
Conclusion: Thinking back, with such a plug-in, node operation is a piece of cake. The three major troubles of node operation mentioned before will be solved. Moreover, this plug-in has nothing to do with any logic and does not require secondary processing. It can be used as soon as you pick it up. It can also be expanded when it cannot meet the needs. Imagine that one day the development process will be like a jigsaw puzzle. After the developed plug-ins are combined, a project will come out. How wonderful it would be. The result may not be as good as expected, but it can be expected. Going in this direction, it is inevitable that events will become simpler and simpler. Because the article is too long, the source code part will be explained in detail next time I publish an article.
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn