写一个求和的函数sum,达到下面的效果
// Should equal 15 sum(1, 2, 3, 4, 5); // Should equal 0 sum(5, null, -5); // Should equal 10 sum(‘1.0‘, false, 1, true, 1, ‘A‘, 1, ‘B‘, 1, ‘C‘, 1, ‘D‘, 1, ‘E‘, 1, ‘F‘, 1, ‘G‘, 1); // Should equal 0.3, not 0.30000000000000004 sum(0.1, 0.2);
function sum() { var nResult = 0; for (var i = 0, l = arguments.length; i < l; i++) { nResult += window.parseFloat(arguments[i]) || 0; } return nResult.toFixed(3) * 1000 / 1000; }
请写一个表格以及对应的CSS,使表格奇数行为白色背景,偶数行为灰色背景,鼠标移上去时为黄色背景。
<table class="table"> <tr><td>第一行</td></tr> <tr><td>第二行</td></tr> <tr><td>第三行</td></tr> <tr><td>第四行</td></tr> </table> <style> .table tr:nth-child(2n-1){ background-color:white; } .table tr:nth-child(2n){ background-color:grey; } .table tr:hover{ background-color:yellow; } </style>
写一个traverse函数,输出所有页面宽度和高度大于50像素的节点。
function traverse(oNode) { var aResult = []; oNode = oNode || document.body; if (oNode.style) { var nWidth = window.parseInt(oNode.style.width, 10) || 0; var nHeight = window.parseInt(oNode.style.height, 10) || 0; if (nWidth > 50 && nHeight > 50) { aResult.push(oNode); } } var aChildNodes = oNode.childNodes; if (aChildNodes.length > 0) { for (var i = 0, l = aChildNodes.length; i < l; i++) { var oTmp = aChildNodes[i]; aResult = aResult.concat(traverse(oTmp)); } } return aResult; }
时间: 2024-10-10 18:21:18