简体   繁体   English

如何使用字符串中的多个变量验证 HTML 内容

[英]How to validate HTML content with multiple variables put inside a string

I've below code我有下面的代码

issuesList.innerHTML += "<div class=\"well\">" +
        "<h6> Issue Id:" + id + "</h6" +
        "<p><span class=\"label label-info\">" + status + "</span></p>" +
        "<h3>" + desc + "</h3>" +
        "<p><span class=\"glyphicon glyphicon-time\">" + severity + "</span></p>" +
        "<p><span class=\"glyphicon glyphicon-user\">" + assignedTo + "</span></p>" +
        "<a href=\"#\"  onclick= \"setStatusClosed(\"" + id + "\")\"   class=\"btn btn-warning\">Close</a>" +
        "<a href=\"#\ onclick=\"deleteIssue( \"" + id + "\")\"  class=\"btn btn-danger\>Delete</a>" +
        "</div>";

I wish to validate if this is correct format of HTML as it contains many variable and it's hard to debug if something goes wrong.我想验证这是否是 HTML 的正确格式,因为它包含许多变量,如果出现问题很难调试。

To make it one degree less complicated, you can use single quotes to wrap class or style declarations and double quotes to wrap the overall html (or vice versa).为了使它更简单一点,您可以使用单引号将 class 括起来,或者使用样式声明和双引号将整个 html 括起来(反之亦然)。

In this piece of html, I observed issued at closing of </h6> and in the class declaration of last anchor tag.在这块 html 中,我观察到 issue at closing of </h6>和 class 最后一个锚标签的声明。

issuesList.innerHTML += "<div class='well'>" +
        "<h6> Issue Id:" + id + "</h6>" +
        "<p><span class='label label-info'>" + status + "</span></p>" +
        "<h3>" + desc + "</h3>" +
        "<p><span class='glyphicon glyphicon-time'>" + severity + "</span></p>" +
        "<p><span class='glyphicon glyphicon-user'>" + assignedTo + "</span></p>" +
        "<a href='#'  onclick= 'setStatusClosed(\"" + id + "\")'   class='btn btn-warning'>Close</a>" +
        "<a href='#' onclick='deleteIssue( \"" + id + "\")'  class='btn btn-danger'>Delete</a>" +
        "</div>";

You may use following function to validate the html so created:您可以使用以下 function 来验证这样创建的 html:

function validator(createdhtml) {
    var tempdiv = document.createElement('div');
    tempdiv.innerHTML = createdhtml;
    return tempdiv.innerHTML;
}

Further I recommend reading: check-if-html-snippet-is-valid此外,我建议阅读: check-if-html-snippet-is-valid

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

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