1

我正在使用 JSON 数据循环遍历列表以构建 HTML 的 LI 行。问题是使用 JQuery 附加时,类名对 Web 浏览器没有明显影响。(就好像类名不存在一样)。

每月计划 #6 没有背景颜色,这意味着未正确使用或附加类名。

这里似乎有什么问题?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title></title>
<style type="text/css">
    li
    {
        margin: 0px 0px 0px 0px;
        border-top: solid 1px #000000;
        border-left: solid 1px #000000;
        border-right: solid 1px #000000;
        padding: 0px 0px 0px 0px;
        list-style: none;
    }
    ul 
    {
        margin: 0px 0px 0px 0px;
        padding: 0px 0px 0px 0px;
        border: solid 0px #000000;
        list-style: none;
    }
    ul li.liFirstChild
    {
        border-bottom: solid 0px #000000;
        -webkit-border-top-left-radius: 14px 14px;
        -webkit-border-top-right-radius: 14px 14px;
        -webkit-border-bottom-left-radius: 0px 0px;
        -webkit-border-bottom-right-radius: 0px 0px;
        -moz-border-radius-topleft: 14px;
        -moz-border-radius-topright: 14px; 
        -moz-border-radius-bottomleft: 0px;
        -moz-border-radius-bottomright: 0px; 
        border-radius: 14px 14px 0px 0px;
    }
    ul li:last-child, ul li.liLastChildTrue
    {
        border-bottom: solid 1px #000000;
        -webkit-border-bottom-left-radius: 14px 14px;
        -webkit-border-bottom-right-radius: 14px 14px;
        -moz-border-radius-bottomleft: 14px;
        -moz-border-radius-bottomright: 14px; 
        border-radius: 0px 0px 14px 14px;
        background-color: #ff0000;
    }
    ul li.liLastChildFalse
    {
        -webkit-border-bottom-left-radius: 0px 0px;
        -webkit-border-bottom-right-radius: 0px 0px;
        -moz-border-radius-bottomleft: 0px;
        -moz-border-radius-bottomright: 0px; 
        border-radius: 0px 0px 0px 0px;
        border-bottom: solid 0px #ffffff;
        background-color: #ff00ff;
    }
</style>
<script type="text/javascript" src="/Member/Scripts/jquery-v2.0.3/jquery-v2.0.3.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var _dataHtmlRow = { "Booklet": [{ "Id": "0", "Schedule": "12 Payments", "Year": "1", "PaymentAmount": "$2.00" }, { "Id": "1", "Schedule": "18 Payments", "Year": "1.5", "PaymentAmount": "$4.00" }, { "Id": "2", "Schedule": "24 Payments", "Year": "2", "PaymentAmount": "$6.00" }, { "Id": "3", "Schedule": "30 Payments", "Year": "2.5", "PaymentAmount": "$8.00" }, { "Id": "4", "Schedule": "36 Payments", "Year": "3", "PaymentAmount": "$10.00" }, { "Id": "5", "Schedule": "42 Payments", "Year": "3.5", "PaymentAmount": "$12.00" }] };

        //Calculate Quick Quote - Button...
        $('#buttonCalculateQuickQuote').on('click', function (e) {
            var htmlRow = "";
            var $divGridViewer = $('#divGridViewer');

            $divGridViewer.empty();

            $divGridViewer.append("<ul>");

            $.each(_dataHtmlRow.Booklet, function (i, o) {
                htmlRow = "";

                htmlRow += "<li " + (i == 0 ? "class='liFirstChild'" : "") + " style='background-color:#FFA94C;'>";
                htmlRow += " <div style='float:left;'>Monthly Plan #" + (i + 1).toString() + "</div>";
                htmlRow += " <div style='clear:both;'></div>";
                htmlRow += "</li>";
                htmlRow += "<li>";
                htmlRow += " <div style='float:left;'>Schedule: </div>";
                htmlRow += " <div style='float:right;'>" + o.Schedule + "</div>";
                htmlRow += " <div style='clear:both;'></div>";
                htmlRow += "</li>";
                htmlRow += "<li>";
                htmlRow += " <div style='float:left;'>Year: </div>";
                htmlRow += " <div style='float:right;'>" + o.Year + "</div>";
                htmlRow += " <div style='clear:both;'></div>";
                htmlRow += "</li>";
                htmlRow += "<li class='" + (i == (_dataHtmlRow.Booklet.length - 1) ? "liLastChildTrue" : "liLastChildFalse") + "'>";
                htmlRow += " <div style='float:left;'>Payment Amt: </div>";
                htmlRow += " <div style='float:right;'>" + o.PaymentAmount + "</div>";
                htmlRow += " <div style='clear:both;'></div>";
                htmlRow += "</li>";

                $divGridViewer.append(htmlRow);
            });

            $divGridViewer.append("</ul>");

            return false;
        });
    });
</script>
</head>
<body>

<div style="padding:25px;">
    <div style="padding-bottom:20px;">
        <input type="button" id="buttonCalculateQuickQuote" value="Calculate Quick Quote" style="" />
    </div>
    <div>
        <div id="divGridViewer">
        </div>
    </div>
</div>

</body>
</html>
4

1 回答 1

3

问题是您混淆了append(). 如果您使用浏览器开发工具检查元素,您将看到 html 如下所示:

<div id="divGridViewer">
    <ul></ul>
    <li class="liFirstChild" style="background-color:#FFA94C;">...</li>
    <li>...</li>
</div>

注意<li>s 在 之后<ul></ul>,它是空的。这是因为$divGridViewer.append("<ul>");不会只是添加"<ul>"为字符串,而是创建一个元素并将其添加到<div>. 下次你追加一些东西时,这将被添加<ul>.

一个简单的解决方法是将 s 创建<ul/>为变量,将<li>s 添加到该变量,然后将其附加到divGridViewer

// create ul
var ul = $("<ul/>");

$.each(_dataHtmlRow.Booklet, function (i, o) {
    htmlRow = "";

    htmlRow += "<li " + (i == 0 ? "class='liFirstChild'" : "") + " style='background-color:#FFA94C;'>";
    ...
    htmlRow += "</li>";

    // add the row to the ul instead
    ul.append(htmlRow);
});

// add ul to the container
$divGridViewer.append(ul);

另一个问题是您正在为每月计划标题设置内联样式。这意味着当您添加类时,背景颜色会被忽略,因为内联样式会覆盖它。您可以强制使用 CSS 颜色,background-color: #ffff00 !important;但不建议这样做。

相反,您应该给它另一个具有橙色背景的类(我可能会选择这个):

ul li.monthly {
    background-color:#FFA94C;
}

或者另一种选择是使用一些 CSS 将其应用于每 4 个项目,除了最后一个:

ul li:nth-child(4n):not(:last-child) {
    background-color:#FFA94C;
}

这是包含这些更改的完整页面

$(document).ready(function () {
    var _dataHtmlRow = { "Booklet": [{ "Id": "0", "Schedule": "12 Payments", "Year": "1", "PaymentAmount": "$2.00" }, { "Id": "1", "Schedule": "18 Payments", "Year": "1.5", "PaymentAmount": "$4.00" }, { "Id": "2", "Schedule": "24 Payments", "Year": "2", "PaymentAmount": "$6.00" }, { "Id": "3", "Schedule": "30 Payments", "Year": "2.5", "PaymentAmount": "$8.00" }, { "Id": "4", "Schedule": "36 Payments", "Year": "3", "PaymentAmount": "$10.00" }, { "Id": "5", "Schedule": "42 Payments", "Year": "3.5", "PaymentAmount": "$12.00" }] };
    
    //Calculate Quick Quote - Button...
    $('#buttonCalculateQuickQuote').on('click', function (e) {
        var htmlRow = "";
        var $divGridViewer = $('#divGridViewer');
        
        $divGridViewer.empty();
        
        var ul = $("<ul/>");
        
        $.each(_dataHtmlRow.Booklet, function (i, o) {
            htmlRow = "";
            
            htmlRow += "<li class='monthly" + (i == 0 ? " liFirstChild" : "") + "'>";
            htmlRow += " <div style='float:left;'>Monthly Plan #" + (i + 1).toString() + "</div>";
            htmlRow += " <div style='clear:both;'></div>";
            htmlRow += "</li>";
            htmlRow += "<li>";
            htmlRow += " <div style='float:left;'>Schedule: </div>";
            htmlRow += " <div style='float:right;'>" + o.Schedule + "</div>";
            htmlRow += " <div style='clear:both;'></div>";
            htmlRow += "</li>";
            htmlRow += "<li>";
            htmlRow += " <div style='float:left;'>Year: </div>";
            htmlRow += " <div style='float:right;'>" + o.Year + "</div>";
            htmlRow += " <div style='clear:both;'></div>";
            htmlRow += "</li>";
            htmlRow += "<li class='" + (i == (_dataHtmlRow.Booklet.length - 1) ? "liLastChildTrue" : "liLastChildFalse") + "'>";
            htmlRow += " <div style='float:left;'>Payment Amt: </div>";
            htmlRow += " <div style='float:right;'>" + o.PaymentAmount + "</div>";
            htmlRow += " <div style='clear:both;'></div>";
            htmlRow += "</li>";
            
            ul.append(htmlRow);
        });
        
        $divGridViewer.append(ul);
        
        return false;
    });
});
li
{
    margin: 0px 0px 0px 0px;
    border-top: solid 1px #000000;
    border-left: solid 1px #000000;
    border-right: solid 1px #000000;
    padding: 0px 0px 0px 0px;
    list-style: none;
}
ul 
{
    margin: 0px 0px 0px 0px;
    padding: 0px 0px 0px 0px;
    border: solid 0px #000000;
    list-style: none;
}
ul li.liFirstChild
{
    border-bottom: solid 0px #000000;
    -webkit-border-top-left-radius: 14px 14px;
    -webkit-border-top-right-radius: 14px 14px;
    -webkit-border-bottom-left-radius: 0px 0px;
    -webkit-border-bottom-right-radius: 0px 0px;
    -moz-border-radius-topleft: 14px;
    -moz-border-radius-topright: 14px; 
    -moz-border-radius-bottomleft: 0px;
    -moz-border-radius-bottomright: 0px; 
    border-radius: 14px 14px 0px 0px;
}
ul li:last-child, ul li.liLastChildTrue
{
    border-bottom: solid 1px #000000;
    -webkit-border-bottom-left-radius: 14px 14px;
    -webkit-border-bottom-right-radius: 14px 14px;
    -moz-border-radius-bottomleft: 14px;
    -moz-border-radius-bottomright: 14px; 
    border-radius: 0px 0px 14px 14px;
    background-color: #ff0000;
}
ul li.liLastChildFalse
{
    -webkit-border-bottom-left-radius: 0px 0px;
    -webkit-border-bottom-right-radius: 0px 0px;
    -moz-border-radius-bottomleft: 0px;
    -moz-border-radius-bottomright: 0px; 
    border-radius: 0px 0px 0px 0px;
    border-bottom: solid 0px #ffffff;
    background-color: #ff00ff;
}

ul li.monthly {
    background-color:#FFA94C;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div style="padding:25px;">
    <div style="padding-bottom:20px;">
        <input type="button" id="buttonCalculateQuickQuote" value="Calculate Quick Quote" style="" />
    </div>
    <div>
        <div id="divGridViewer">
        </div>
    </div>
</div>

于 2015-01-19T21:31:12.073 回答