cun*_*yvz -1 html javascript web
有些网站使用以下JavaScript行来构建网站:
document.write('<link rel="stylesheet" type="text/css" href="' + staticpath +
'resources/css/mobile-android.css" /><div class="overlay"></div>
<div class="new-folder-popup" id="message"><div class="new-folder-popup-bg"><div
class="new-folder-header">MEGA for Android</div><div class="new-folder-main-bg">
<div class="new-folder-descr">Do you want to install the latest<br/> version of the MEGA app for Android?</div><a class="new-folder-input left-b/>');
Run Code Online (Sandbox Code Playgroud)
HTML标记是通过vanilla JavaScript生成的,不使用任何库.这段代码是由程序员生成还是编写的?什么样的方法使用这种生成HTML的方式?
我也不知道顺便确定这些是否可行.
你不应该使用document.write.你会发现很多在线推荐的文章和原因.在这里,我将向您展示通过JavaScript生成HTML的3种方法,我个人使用这三种方法.
这个方法很简单并且运行良好但是当必须在JS代码中键入HTML时它很容易出错.它还混合了HTML和JS,这不是一个好的做法.不过,它可以工作,我将这种方法用于简单的项目.
请注意${some_var}语法.我刚刚想出了它并不是特定于JavaScript.我们将使用JavaScript的replace()方法和简单的正则表达式将这些占位符替换为实际值.
// A function that returns an HTML string - a.k.a. a Template
function getHtml() {
var html = '<div class="entry">';
html += '<h3 class="title">${title}</h3>';
html += '<time>';
html += '<span class="month">${month}</span> ';
html += '<span class="day">${day}</span>, ';
html += '<span class="year">${year}</span>';
html += '</time></div>';
return html;
}
// Helper function that takes an HTML string & an object to use for
// "binding" its properties to the HTML template above.
function parseTemplate(str, data) {
return str.replace(/\$\{(\w+)\}/gi, function(match, parensMatch) {
if (data[parensMatch] !== undefined) {
return data[parensMatch];
}
return match;
});
}
// Now parse the template
parseTemplate(getHtml(), {
title: 'Lorem Ipsum',
month: 'January',
day: '16',
year: '2015'
});
Run Code Online (Sandbox Code Playgroud)
输出:
"<div class="something"><h3 class="title">Lorem Ipsum</h3><time><span class="month">January</span> <span class="day">16</span>, <span class="year">2015</span></time></div>"
Run Code Online (Sandbox Code Playgroud)
这种方法涉及使用各种DOM方法,document.createElement()并且也很有效.缺点是它可能是重复的,但您可以随时创建自己的API,就像我们使用tag()下面的函数一样.
// Helper function to create an element with attributes
function tag(name, attrs) {
var el = document.createElement(name.toString());
!!attrs && Object.keys(attrs).forEach(function(key) {
el.setAttribute(key, attrs[key]);
});
return el;
}
// Now create some DOM nodes
var li = tag('li', {'id': 'unique123', 'data-id': 123, 'class': 'item active'});
var p = tag('p');
// Add text to the paragraph and append it to the list item
p.textContent = 'Lorem ipsum dolor'; // Not cross-browser; more here: https://developer.mozilla.org/en-US/docs/Web/API/Node.textContent
li.appendChild(p);
// Append the list item to the body
document.body.appendChild(li);
// Or append it somewhere else
document.getElementById('output-div').appendChild(li);
document.querySelector('.content').appendChild(li);
// Other DOM methods/props you can use include:
li.innerHTML = 'some html string';
var txt = document.createTextNode('hello');
// and more...
Run Code Online (Sandbox Code Playgroud)
使用此方法,我们使用模板系统,如Handlebars(http://handlebarsjs.com/).当您预计项目中有很多模板时,它运行良好.这些模板实际上也可以预先编译成JavaScript函数,而不是像下面这样的脚本标记,强烈建议这样做.
<!-- An HTML template made out of script tags inside your page. -->
<script id="entry-template" type="text/x-handlebars-template">
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
</script>
Run Code Online (Sandbox Code Playgroud)
现在编译模板:
// Get the HTML source
var source = document.getElementById('entry-template').innerHTML;
// Compile it - `template` here is a function similar to `getHtml()` from the first example
var template = Handlebars.compile(source);
// Provide some data to the template.
var html = template({title: "My New Post", body: "This is my first post!"});
Run Code Online (Sandbox Code Playgroud)
在HTML变量现在包含以下内容,你可以使用类似将其插入到你的页面innerHTML:
<div class="entry">
<h1>My New Post</h1>
<div class="body">
This is my first post!
</div>
</div>
Run Code Online (Sandbox Code Playgroud)