简体   繁体   English

嵌套HTML5 <template> 标签

[英]Nesting HTML5 <template> tags

Is there a way to nest HTML5 < template > tags inside each other? 有没有一种方法可以将HTML5 <template>标签彼此嵌套在一起?

Looking for an easy way to update my page with universal code so that every page does not require a lot of JS to function. 寻找一种使用通用代码更新我的页面的简便方法,以便每个页面不需要大量的JS即可运行。

For instance the following code: 例如下面的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <a href="#" class="link">Load Template</a><br>

    <template id="profileview">
        <h1>Employee</h1>
        <template id="employeename">
            <div>
                <label for="FirstName">First Name:</label>
                <input type="text" name="FirstName" id="FirstName" value="">
            </div>
            <div>
                <label for="MiddleInitial">Middle Initial:</label>
                <input type="text" name="MiddleInitial" id="MiddleInitial" value="">
            </div>
            <div>
                <label for="LastName">Last Name:</label>
                <input type="text" name="LastName" id="LastName" value="">
            </div>
        </template>
    </template>
    <script src="/media/js/jquery/jquery-1.9.1.js"></script>
</body>

JS file: JS档案:

$('.link').click(function(event) {
  event.preventDefault();
  $.ajax({
    url: '/codes/html5/template.json.php',
    dataType: 'json'
}).done(function(result) {
var body = $('body');
    $.each(result.templates, function(i, t) {
  var template = document.querySelector('#'+t.name);

  if (t.subtemplates) {
    $.each(t.subtemplates, function(j, t2) {
     // subtemplate processing code here
    });
  }
  var clone = document.importNode(template.content, true);
  body.append(clone);
});
}); 

}); });

JSON file: JSON文件:

{
"templates" : [
    {
        "name" : "profileview",
        "subtemplates" : [
            {
                "name" : "employeename"
            }
        ]
    }
]
}

Is there a way to nest HTML5 < template > tags inside each other? 有没有一种方法可以将HTML5 <template>标签彼此嵌套在一起?

According to html5rocks you can nest <template> tags. 根据html5rocks,您可以嵌套<template>标签。

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

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