xiaotuzi 上海海事大学 2020年04月17日
trim get()函数不明白
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<title>Developing Plugins</title>

<link rel="stylesheet" href="08.css" type="text/css" />
<link rel="stylesheet" href="ui-themes/smoothness/jquery-ui-1.10.0.custom.css" type="text/css" />

<script src="jquery.js"></script>
<script src="jquery-ui-1.10.0.custom.min.js"></script>
<script src="./listings/8.3.js"></script>
</head>
<body>
<div id="container">
<h1>Inventory</h1>
<table id="inventory">
<thead>
<tr class="one">
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tfoot>
<tr class="two" id="sum">
<td>Total</td>
<td></td>
<td></td>
</tr>
<tr id="average">
<td>Average</td>
<td></td>
<td></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><a href="spam.html" data-tooltip-text="Nutritious and delicious!">Spam</a></td>
<td>4</td>
<td>2.50</td>
</tr>
<tr>
<td><a href="egg.html" data-tooltip-text="Farm fresh or scrambled!">Egg</a></td>
<td>12</td>
<td>4.32</td>
</tr>
<tr>
<td><a href="gourmet-spam.html" data-tooltip-text="Chef Hermann's recipe.">Gourmet Spam</a></td>
<td>14</td>
<td>7.89</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

8.3.js 的内容
/******************************************************************************
Our plugin code comes first in this document. Normally, plugins would
appear in separate files named jquery.plugin-name.js, but for our examples
it's convenient to place this plugin code in the same JavaScript file as
the code that calls it.
******************************************************************************/

/******************************************************************************
$.sum()
Return the total of the numeric values in an array/object.
******************************************************************************/
(function($) {
$.sum = function(array) {
var total = 0;

$.each(array, function(index, value) {
value = $.trim(value);
value = parseFloat(value) || 0;

total += value;
});
return total;
};
})(jQuery);


/******************************************************************************
End plugin code; begin custom script code.
******************************************************************************/
$(document).ready(function() {
var $inventory = $('#inventory tbody');
var quantities = $inventory.find('td:nth-child(2)')
.map(function(index, qty) {
return $(qty).text();

}).get();

var sum = $.sum(quantities);
$('#sum').find('td:nth-child(2)').text(sum);
});

老师好,这里的 .trim() 和 .get()是什么意思呢?谢谢老师大大
...全文
118 点赞 收藏 5
写回复
5 条回复

还没有回复,快来抢沙发~

发动态
发帖子
JavaScript
创建于2007-09-28

5.1w+

社区成员

22.3w+

社区内容

Web 开发 JavaScript
社区公告
暂无公告