简体   繁体   English

在javascript中添加HTML代码段?

[英]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. 我正在为网站使用JavaScript插件,必须在其中将表单添加到页面的特定面板中。 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. 我想知道是否有任何方法可以将html文件(包含表单的html代码)导入/读取到javascript文件中,而不是创建一堆HTML元素来创建表单。

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). 如果使用jQuery,则可以使用appendinsertAfter和类似方法append HTML作为字符串插入(请注意,模板字符串语法仅在兼容ECMAScript6的浏览器上有效)。

$('#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>
  `);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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