gpt4 book ai didi

javascript - 将包含类名的 html 脚本附加到 div 标签没有效果(没有明显的 css 渲染)

转载 作者:行者123 更新时间:2023-11-30 12:30:03 24 4
gpt4 key购买 nike

我正在使用 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>

最佳答案

问题是您混淆了 append() 的目的.如果您使用浏览器开发工具检查元素,您将看到 html 看起来像这样:

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

请注意 <li><ul></ul> 之后,这是空的。这是因为 $divGridViewer.append("<ul>");不会只添加 "<ul>"作为字符串,但创建一个元素并将其添加到 <div> .下次您添加内容时,这将被添加到 <ul> 的末尾之后 .

一个简单的解决方法是创建 <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);

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

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

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>

关于javascript - 将包含类名的 html 脚本附加到 div 标签没有效果(没有明显的 css 渲染),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28033147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com