我對JavaScript很陌生,有人幫我編寫了這個腳本,它在Chrome上效果很好,但它在Firefox中不起作用,並且還沒有在IE中測試過,但我希望它能夠在所有瀏覽器,我真的不知道那的jQuery高達翻譯它什麼在這裏混淆了我的跨瀏覽器功能?
function displayTotal()
{
var tableRows = document.getElementById('budgetTable').getElementsByTagName('tr');
var totalDays = tableRows.length - 3; //Don't count the header rows and the Total rows
var totalPrice = 0;
var price = filterNum(document.getElementById('txtPrice').value);
var totalField = document.getElementById('txtTotal');
var tempHours = 0;
var tempTotal = 0;
for(var i = 0; i < totalDays; i++)
{
tempHours = document.getElementById("end" + i).value - document.getElementById("start" + i).value;
tempTotal = tempHours * price;
document.getElementById("total" + i).innerHTML = formatCurrency(tempTotal);
totalPrice += tempTotal;
console.log(i, "Start:" + document.getElementById("start" + i).value, "End:" + document.getElementById("end" + i).value, "Hours:" + tempHours, "Total:" + tempTotal);
}
totalField.value = formatCurrency(totalPrice);
}
function addRowToTable()
{
var tbl = document.getElementById('budgetTable');
var lastRow = tbl.rows.length - 2;
var iteration = lastRow;
var entry = iteration - 1; //because we started with day0, etc
var row = tbl.insertRow(lastRow);
// day cell
var cellDay = row.insertCell(0);
cellDay.appendChild(createInput('text','day' + entry, '', displayTotal));
// start cell
var cellStart = row.insertCell(1);
cellStart.appendChild(createInput('text','start' + entry, 0, displayTotal));
// end cell
var cellEnd = row.insertCell(2);
cellEnd.appendChild(createInput('text','end' + entry, 0, displayTotal));
// total cell
var cellTotal = row.insertCell(3);
cellTotal.id = 'total' + entry;
}
function createInput(type, id, value, action)
{
var el = document.createElement('input');
el.type = type;
el.id = id;
el.value = value;
el.onkeyup = action;
return el;
}
function filterNum(str)
{
re = /^\$|,/g;
// remove "$" and ","
return str.replace(re, "");
}
function formatCurrency(num)
{
num = isNaN(num) || num === '' || num === null ? 0.00 : num;
return parseFloat(num).toFixed(2);
}
任何幫助將受到歡迎,因爲我真的不知道我失去了再點這裏。
編輯:好吧,這很奇怪,但在Firefox上,當我啓用螢火蟲試圖調試它時,...它的工作原理。
@ la_f0ka:您在Firefox中遇到什麼錯誤?你之前用過螢火蟲嗎? – shane87 2011-05-06 14:30:50
'totalDays = tableRows.length-3'是多餘的。如果你想跳過'header'行,定義一個thead和tbody。然後,你可以將一個id放在tbody上並獲取這些行或者getElementById('budgetTable')。getElementsByTagName(「tbody」)[0] .length – Gary 2011-05-06 14:36:26
「It does not work」is * never * a good error description。請告訴出了什麼問題,預期的行爲是什麼,以及如果您在控制檯中收到錯誤消息。 – 2011-05-06 14:37:03