In the process of writing the front-end code, often found that there are many functions of the code is repeated, for example, in the home page of NetEase, there are several tabs, the function of these tabs is the same, that is, when the mouse clicks or strokes the past, the following content display will display corresponding content, The difference is the number of tabs and the text content of the tabs. So in the process of writing this part of the code, we can reuse the code as much as possible, without causing too much redundant code.
Reusing code:
1, as far as possible to ensure that the HTML code structure consistent, you can select child elements through the parent;
2, the core of the main program to achieve, wrapped up with functions;
3, to find out the different values in each group, through the transfer of parameters to achieve;
Here is a simple example, for example, when we are in the statistical price of goods, we only know the price of goods and the quantity of goods can directly calculate the total price of this product, then for different products, the calculation method is the same, this time we can reuse code, Without having to write too much code to implement this functionality. As shown in the figure:
Implementation code:
<!
DOCTYPE html> Description
1, the number of goods should be directly read the number of pages set, because different site preset initial value may not be the same, such as 0 or 1;
2, in the reduction of goods, pay attention to determine whether the quantity of goods is <0, because the number of <0, then the commodity price is negative, that is to pay the money ... ;