Javascript

Chaining getElementById

getElementById本身无法连续使用,建议用querySeletor替换掉

Element.prototype.getElementById = function(id) {
    return this.querySelector("#"+id);
}

  

getElementsByName

Element.prototype.getElementsByName = function(name) {
  return this.querySelectorAll("[name=""+name+""]");
}
  

  

为表添加一个可以替换的行

  function add_info_tr(name, text_value, table_ele) {
    if(table_ele.getElementsByName(name).length > 0){
      var row = table_ele.getElementsByName(name)[0];
      var cells = row.getElementsByTagName("td");
      cells[0].innerHTML = normalStringToElementP(name);
      cells[1].innerHTML = normalStringToElementP(text_value);
    }else{
      var row = table_ele.insertRow();
      row.setAttribute("name", name);
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      cell1.innerHTML = normalStringToElementP(name);
      cell2.innerHTML = normalStringToElementP(text_value);
    }
  }

  

原文地址:https://www.cnblogs.com/xuesu/p/13926832.html