1

此代码生成一个分类帐。我把它缩小到最低限度。通过单击加号,它会在分类帐中添加一个额外的行。

我正在寻找添加变量的每个总计newAmount并将其更新的总计定位在每行右侧的 TD 中。我创建newAmount.id = "mainAmount";创建唯一 ID 认为这会有所帮助。

var mainNumber = 0;

function addElement()
{

  //add a number for each row
  mainNumber++;


  //create each row, id each slot, and add it where it goes
  newDiv = document.createElement("div");
  newDiv.id = "main";
  newTable = document.createElement("table");
  newTable.id = "mainTable";
  newDiv.appendChild(newTable);
  newTr = document.createElement("tr")
  newTr.id = (mainNumber);
  newTr.className = "mainRow";
  newTable.appendChild(newTr);
  newAmount = document.createElement("td");
  newAmount.id = "mainAmount";
  newAmount.className = (mainNumber);
  newPlus = document.createElement("td");
  newPlus.id = "mainPlus";
  newTotalTable = document.createElement("table");
  newDiv.appendChild(newTotalTable);
  newTotalTable.id = "mainTotalTable";
  newTotalTr = document.createElement("tr");
  newTotalTable.appendChild(newTotalTr);
  newTotalTr.id = "mainTotalTr";
  newTotalTd = document.createElement("td");
  newTotalTd.id = "mainTotalTd" + (mainNumber);
  newTr.appendChild(newAmount);
  newTotalTr.appendChild(newTotalTd);

  //whats default inside of each slot
  newAmount.innerHTML = '<form name="formAmount"><textarea name="textAmount" size="25" onfocus="wait();" id="amount' + (mainNumber) + '">0</textarea>';
  newTr.appendChild(newPlus);

  //click this to add a row
  newPlus.innerHTML = '<a href="#" onclick="addElement();"><img src="images/plus.png"></a>';


  // add the newly created element and it's content into the DOM
  my_div = document.getElementById("mainAnchor");
  document.body.insertBefore(newDiv, my_div);

}

//doesn't work...trying to hover over any row and show var idName in console  
function trHover(){ 
$('tr').hover(function() {
    var idName = $('tr'+'#'+(mainNumber)).attr('id');                    
   console.log(idName);
});
}

//when you focus on amount box, this is activated, which adds attribute onblur and stars addTotal
function wait(){
var blurred = $(this).attr("onblur");
blurred = addTotal();
}

//adds total and displays it in td to the right
function addTotal(){
var y = 1;
var sum = 0;
var input;
while( ( input = document.getElementById( 'amount'+y ) ) ) {
    sum += parseInt( input.value );
    ++y;
    console.log(sum);
    $("#mainTotalTd1").text(sum);
}
}
4

1 回答 1

0

与其添加一行,不如单击加号添加一个 div 和两个表,所以我不确定你要去那里。不过,我可以修复悬停功能:

$('tr').hover(function() {
    var idName = $(this).attr('id'); // 'this' is the element being hovered
    console.log(idName);
});
于 2012-09-13T01:40:31.843 回答