Dynamically create HTML elements based on jquery

Source: Internet
Author: User
Tags html form

In the development of Web front-end, often encountered some data or type can not be determined before running the situation, at this time, the data display, with the help of dynamic creation of HTML elements to show.

The common way to dynamically create HTML elements is as follows, roughly the same!

HTML functions:

1 var elehtml = "<div id= ' newone ' > XXX </div> '"; 2 jQuery (your_specified_selector). HTML (elehtml)

Append function:

1 var elehtml = "<div id= ' Newone ' >xxx</div>"; 2 jQuery (your_specified_selector). Append (elehtml);

Appendto function:

var elehtml = "<div id= ' Newone ' >xxx</div>", JQuery. ( elehtml). AppendTo (your_specified_selector);

However, the dynamic existence of HTML elements, there is no way through the regular click function to give jquery to its binding function response, need to be implemented in the following way (the following is an example of the fragment code in my Project):

1 $ ("#userinfopanel"). Click (function(e) {2     if($ (E.target). is ("# Clear ")) {3             //your logic4     }else{ 5             // your logic 6     }    7 }

In addition, jquery Validate has no way of verifying the legitimacy of dynamically generated HTML form elements. Need to implement it yourself.

Dynamically create HTML elements based on jquery

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.