gpt4 book ai didi

javascript - 显示 JavaScript

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

我仍然停留在看似简单的概念上。我有一个非常非常简单的 javascript,它只是做了一些添加,我想在网页上显示结果。我试过使用标签,但它似乎没有任何作用,现在我正在尝试使用 html 中的 ,但它也不起作用。这看起来应该是一件很简单的事情,但我显然遗漏了一些东西。任何帮助将不胜感激。我附上了 html 代码、脚本和 css。

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="UTF-8" />
<title>The Lighthouse</title>
<link href="lhouse.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="title">
<p><img src="logo.jpg" alt="The Lighthouse" />
</p>
<p>The Lighthouse<br />
543 Oak Street<br />
Owensboro, KY &nbsp;&nbsp;42302<br/>
(270) 555-7511
</p>
</div>

<div id="data_list">
<table rules="rows" cellspacing='0'>
<thead>
<tr>
<th>Date</th>
<th>Amount</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td>2011-09-18</td>
<td id="amount1">125</td>
<td>Nina</td>
<td>Largent</td>
<td>88 Regal Lane<br />Willaimsburg, KY 40789</td>
</tr>
<tr class="yellowrow">
<td>2011-09-18</td>
<td id="amount2">75</td>
<td>Mike</td>
<td>Hunt</td>
<td>404 Barrow Street<br />London, KY 40742</td>
</tr>
<tr>
<td>2011-09-16</td>
<td id="amount3">50</td>
<td>Monica</td>
<td>Lang</td>
<td>743 Stawlings Drive<br />Danville, KY 40423</td>
</tr>
<tr class="yellowrow">
<td>2011-09-15</td>
<td id="amount4">150</td>
<td>William</td>
<td>McKnight</td>
<td>404 Barrow Street<br />Danville, KY 40423</td>
</tr>
<tr>
<td>2011-09-14</td>
<td id="amount5">250</td>
<td>Latrina</td>
<td>Hults</td>
<td>750 Whitehall Road<br />London, KY 40742</td>
</tr>
<tr class="yellowrow">
<td>2011-09-13</td>
<td id="amount6">50</td>
<td>Danny</td>
<td>Shamblin</td>
<td>123 Smith Street<br />Owensboro, KY 42303</td>
</tr>
</tbody>
</table>
</div>

<div id="totals">
<table rules="groups" cellspacing="1">
<thead>
<tr>
<th id="sumTitle" colspan="2">Summary</th>
</tr>
</thead>
<tbody>
<tr>
<th>Contributors</th>
<td id="contributions"> </td>
</tr>
<tr>
<th>Amount</th>
<td id="amount"> </td>
</tr>
</tbody>
</table>
</div>

</body>
</html>

__

// JavaScript Document
window.onload = function ()
{
//find the div tags called amount1, amount2, ...amount6
var amount1 = 125;
var amount2 = 75;
var amount3 = 50;
var amount4 = 150;
var amount5 = 250;
var amount6 = 50;
var totalAmount = amount1 + amount2 + amount3 + amount4 + amount5 + amount6;
var totalContributors = 6;

$("contributions").value = totalAmount.toFixed(2);

}

__

#title {
width: 600px;
text-align:right;
color: rgb(192,142,90);
border-bottom: 1px solid rgb(232,182,130);
margin-bottom:9px;
font-size:10pt;
height: 100px;
}

#title img {
float: left;
}

#data_list {
float: left;
}

table {
font-size: 8pt;
font-family: Arial, Helvetica, sans-serif;
border: 1px solid brown;
margin-right: 20px;
}

.yellowrow {
background-color: yellow;
}

th {
color: white;
background-color: brown;
padding: 2px 5px;
}

td {
vertical-align: top;
padding: 2px 5px;
}

.amt {
text-align: right;
}

#totals table {
font-size: 12pt;
border: solid black 1px;
}

#totals table th {
text-align: left;
}

#totals table td {
text-align: right;
width: 75px;
}

#totals table #sumTitle {
text-align: center;
background-color: yellow;
color: black;
}

最佳答案

在下面一行:

$("contributions").value = totalAmount.toFixed(2);

你忘记了 # 因为 contributions 是 td 的 id 所以使用这个:

$("#contributions").html(totalAmount.toFixed(2)); 

.value 是 javascript,您使用的是 jQuery,因此请使用 .html 函数而不是 .value

并且您还没有在您的 HTML 页面中添加 jquery 库,也添加 jquery 库。如果你的 JS 在不同的文件中,那么也在代码中添加该文件。

使用这个:

<script type="text/javascript" src="your/file/path/*.js"></script>

关于javascript - 显示 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23655185/

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