In a well-structured web page, not only must the structure and performance be separated, but also the structure and logic should be separated. The logic here usually refers to the JS script used in the page, for example: function toggledl (){ // Statement } VaR DL = Document. getelementbyid ("commentslist "); DL. onclick = toggledl; This is an example of structure separation. It does not insert onclick directly into HTML.CodeAnd then add the statement to trigger the click event through the DOM model. In this Code, onclick is an event handle, and its value should be a function body. When you click it, this function body will be run. Therefore, the preceding statement cannot be written as DL. onclick = toggledl (); Or: DL. onclick = "toggledl ()";The former assigns the return value of toggledl () to DL. onclick, and the latter assigns a string to DL. onclick, which is not what we need. From this code, we can see that it is impossible to pass a function with parameters to a trigger.ArticleWhen it comes to webpage structure and logical page separation, it will be mentioned that it is only valid for functions without parameters. To say the parameter, there is actually a "This" pointer, which can be used in the function body to represent the object that calls it. However, when I designed this blog, I encountered a situation where I had to pass parameters. That is, the blog list on the right was obtained from the server through XMLHTTP, an XML document is obtained and instantly parsed into An XHTML document. At this time, the button for viewing comments and posting comments must be bound to the click event, and this event function must also pass the log ID as the parameter. Writing tag attributes directly is not feasible. It can only be implemented using triggers. However, the trigger assignment does not have any parameters. After thinking about this for a long time, the trigger finally worked hard and got it done for me. The solution is as follows: The value of DL. onclick = toggledl (); is actually the return value of the toggledl () function, that is, toggledl () has been run. So why not return a function to DL. onclick? Use the parameters passed by the shell function in the returned function. Think of this, I'm excited and tried it. The Code is as follows: function toggledl (TEST ){ Return function (){ Alert (test ); //...... }} The logic of the event operation is executed in the returned function body, and the parameter is passed in the function that wraps it. In this example, test is used as the parameter. The event binding statement can be written as follows: DL. onclick = toggledl (12); // in this way, the alert (TEST) function is used as the returned value. The returned value is alert (12 ); // DL. onclick = toggledl (); the return value of this function is assigned to DL. onclick. When DL is clicked, the parameter is passed correctly. Another example is:
Function beginadding () {A * = 5; Return function finishadding (B) {alert (a + B );} } VaR add = beginadding (10); // finishadding (B) {alert (a + B );}
Add (20 ); |