html - 附加带换行符的 html 比附加不带空格的 html 更容易吗?

标签 html jquery

我问自己哪种语法更轻,或者两者之间没有区别:

const html = '
<div class="container">
    <div>
        <input type="text" placeholder="login" />
    </div>
    <div>
        <input type="password" placeholder="password" />
    </div>
</div>
'

$('#global_container').append(html)

还有这个:

const html = 
'<div class="container">'
    + '<div>'
        + '<input type="text" placeholder="login" />'
    + '</div>'
    + '<div>'
        + '<input type="password" placeholder="password" />'
    + '</div>'
+ '</div>'

$('#global_container').append(html)

在我的项目中,如果用户按下修改按钮,我需要添加大量的 html。这就是为什么我想知道如何在 js 文件中编写 html 代码。

最佳答案

最好的选择是使用模板文字,因为这提供了各种选项。如果将来需要,您可以轻松使用动态数据(来自 API 等的数据)。

const html = `
<div class="container">
    <div>
        <input type="text" placeholder="login" />
    </div>
    <div>
        <input type="password" placeholder="password" />
    </div>
</div>
`;

检查MDN DOCS :

针对您的问题,我相信第一个选项会更好,因为我们不必采用不同的串联过程,这显然是一项时间复杂的任务。

关于html - 附加带换行符的 html 比附加不带空格的 html 更容易吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72642604/

相关文章:

javascript - 使用内部表填充动态表 - JavaScript

html - 如何在所有其他对话框之上显示对话框?

jquery - 如何使用jquery文件上传插件在mysql中插入带有id的图像?

jquery - 在时间戳内显示和隐藏消息框

html - 重置 WordPress 中所有内容的边距和填充

html - 在 3 列显示中叠瓦式 float

html - 下拉菜单在 Ebay 列表中的任何 IE 中不起作用

javascript - jQuery:如果元素为空,不包括空格和注释

javascript - 使用 CSS 为生成的列表添加动画

javascript - 如何将两个div合并为一个div