Determine whether it is a form Element

Source: Internet
Author: User
Tags button type

A bug in jquery1.41 was found. John resig needs to determine whether the current element is a form element when implementing its event proxy system (live.

 
VaR formelems =/textarea | input | select/I; // *********** formelems. Test (ELEM. nodename)

But obviously, he forgot the button tag. I don't know how others use it. When I design a station text message system, I will use it to identify whether it is a group, or whether it is thrown into a garbage bin or something. It is easy to fix this bug, that is, to add the button field, but I come up with another more rigorous judgment:

 
VaR isformelement = function (OBJ) {return !! (Obj. tagname & "name" in OBJ & "form" in OBJ );}

First, we determine whether it is an element node and use the tagname. In ie, the Comment Node also has a tagname, always "! ", But it doesn't matter. The following conditions can be removed. Then, determine whether the name attribute exists in the prototype. Tag a also has the name attribute, but it does not matter. Finally, determine whether the prototype has the form attribute. Each form element has a form attribute pointing to its closest ancestor form tag. However, form elements are not only input tags, select tags, textarea tags, button tags, but also label tags, fieldset tags, legend tags. HTML5 also adds output tags and datalist tags, who knows what else to add. But now we can know that label, fildset, and legend won't add anything at the time of submission. (When submitting, A querystring is generated and submitted to the backend in the form of name = Value & name2 = value2. The name attribute works at this time ). Because none of them have the name attribute, we can differentiate them!

Finally, let's verify it through the following web pages:

 
    
    isformelement by situ zhengmei    
   
   
    
     form  
      1 
        2   3   
       
    
situ zhengmei
submit

<Br/> <! Doctype HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "> <br/> <SCRIPT src =" http://common.cnblogs.com/script/jquery.js "type =" text/JavaScript "> </SCRIPT> <br/> <SCRIPT type =" text/JavaScript "> <br/> var isformelement = function (OBJ) {<br/> return !! (Obj. tagname & "name" in OBJ & "form" in OBJ); <br/>}</P> <p >$ (function () {<br/> alert ("judgment comment" + isformelement ($ ("body") [0]. firstchild) <br/> alert ("judgment form" + isformelement ($ ("form") [0]); <br/> alert ("determining Div" + isformelement ($ ("Div") [0]); <br/> alert ("select" + isformelement ($ ("select") [0]); <br/> alert ("determining label" + isformelement ($ ("label") [0]); <br/> alert ("button" + isformelement ($ ("button") [0 ]); <Br/> alert ("determining input" + isformelement ($ ("input") [0]); <br/> alert ("determining fieldset" + isformelement ($ ("fieldset") [0]); <br/> alert ("determining Legend" + isformelement ($ ("legend") [0]); <br/> }); </P> <p> </SCRIPT> <br/> <title> isformelement by situ zhengmei </title> <br/> </pead> <br/> <body> <br/> <! -- This is a comment node // --> </P> <p> <form action = ""> <br/> <fieldset> <br/> <legend> form </legend> <br/> <select name = "AAA"> <br/> <option> 1 </option> <br/> <option> 2 </option> <br/> <option> 3 </option> <br/> </SELECT> <br/> <label for = "BBB"> <br/> <input type = "text" name = "BBB"> <br/> </label> <br/> <textarea name = "CCC"> situ zhengmei </textarea> <br/> <div> <button type = "Submit" name = "DDD"> submit </button> </div> </P> <p> </fieldset> </P> <p> </form> <br/> </body> <br/> </ptml> <br/>

RunCode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.