简体   繁体   中英

Nesting HTML5 <template> tags

Is there a way to nest HTML5 < template > tags inside each other?

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.

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:

$('.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:

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

Is there a way to nest HTML5 < template > tags inside each other?

According to html5rocks you can nest <template> tags.

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