简体   繁体   中英

Adding an html code snippet to javascript?

I am working on a javascript plugin for a website where I have to add a form to a certain panel of the page. I was wondering if there was any way to import/read an html file(it contains the html code for the form) into a javascript file rather than creating a bunch of HTML Elements to create the form.

If using jQuery, you can insert the HTML as a string using the append , insertAfter , and similar methods (note that the template string syntax only works on ECMAScript6 compatible browsers).

$('#addForm').click(function() { // execute the following with #addForm is clicked
  $('#formPanel').append( // add the following to the innerHTML of #formPanel
    // backtick starts a template string
    `
    <form action='sumbit.php' method='POST'>
      First name: <input name='first'>
      Last name: <input name='last'>
      <input type='submit'>
    </form>
  `);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM