TD 可以居中和左对齐,但不能右对齐
我的代码动态添加表格的一行。对齐方式适用于第一行(静态行),但不适用于其他行。相反,我已经测试了具有中心对齐属性的动态内容,并且它有效。
这是我引用的代码部分,下面是完整的函数。
colsfila=document.createElement('td');
textonodo=document.createTextNode(qTrib);
idnodo='qTrib'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.setAttribute('style', 'text-align: center;');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
textonodo=document.createTextNode(vUnTrib);
idnodo='vUnTrib'+NUM_FILAS;
var align='rigth';
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.appendChild(textonodo);
colsfila.setAttribute('style','text-align :rigth;');
novafila.appendChild(colsfila);
function importxmlcompra(){
var novostring= new String(document.getElementById('file').value);
if (novostring!=''){
oPed.open("GET",novostring, false);
oPed.send();
var result = oPed.responseXML;
if (!result.documentElement && oPed.responseStream) {
result.load(oPed.responseStream);
}
}else{
alert ("Por favor seleccione un arquivo XML");
}
if (!result||result.getElementsByTagName('emit').length==0){
return};
var emit=result.getElementsByTagName("emit")[0];
var dEmi=result.getElementsByTagName('dEmi')[0].childNodes[0].nodeValue;
dEmi=dataNF(dEmi);
document.getElementById('dEmi').value=dEmi;
var CNPJ=emit.getElementsByTagName('CNPJ')[0].childNodes[0].nodeValue;
document.getElementById('cnpj').value=CNPJ;
var razaosocialimportada=emit.getElementsByTagName("xNome")[0].childNodes[0].nodeValue;
document.getElementById("xNome").value=razaosocialimportada;
var ide=result.getElementsByTagName("ide")[0];
var nNF=ide.getElementsByTagName("nNF")[0].childNodes[0].nodeValue;
document.getElementById("nNF").value=nNF;
var vNF='R$'+number_format(result.getElementsByTagName("vNF")[0].childNodes[0].nodeValue,2,',','.');
document.getElementById("vNF").value=vNF;
var detlength=result.getElementsByTagName("det").length;
var j=0, cProd, xProd, vUnTrib, qTrib, vProd;
var tabledet=document.getElementsByTagName('table')[0];
var tablerows=document.getElementsByTagName('table')[0].rows.length;
var novafila, colsfila, idnodo, textonodo, imgnodo, attid, ant;
while(j<detlength){
det=result.getElementsByTagName('det')[j];
cProd=det.getElementsByTagName('cProd')[0].childNodes[0].nodeValue;
xProd=det.getElementsByTagName('xProd')[0].childNodes[0].nodeValue;
vUnTrib=number_format(det.getElementsByTagName('vUnTrib')[0].childNodes[0].nodeValue,2,',','.');
qTrib=number_format(det.getElementsByTagName('qTrib')[0].childNodes[0].nodeValue,0,'','.');
vProd=number_format(det.getElementsByTagName('vProd')[0].childNodes[0].nodeValue,2,',','.');
if(validacProd(cProd)==true){
if (tablerows==2&& document.getElementById('cProd0').innerHTML=='0'){
document.getElementById('cProd0').innerHTML=cProd;
document.getElementById('xProd0').innerHTML=xProd;
document.getElementById('vUnTrib0').innerHTML=vUnTrib;
document.getElementById('qTrib0').innerHTML=qTrib;
document.getElementById('vProd0').innerHTML=vProd;
}else{
novafila=document.createElement('tr');
textonodo=document.createTextNode(cProd);
attid=document.createAttribute('id');
colsfila=document.createElement('td');
idnodo='cProd'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
tabledet.appendChild(novafila)
colsfila=document.createElement('td');
textonodo=document.createTextNode(xProd);
idnodo='xProd'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
idnodo='codproduto'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('style','display:none');
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
idnodo='descproduto'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
textonodo=document.createTextNode(qTrib);
idnodo='qTrib'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.setAttribute('style', 'text-align: center;');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
textonodo=document.createTextNode(vUnTrib);
idnodo='vUnTrib'+NUM_FILAS;
var align='rigth';
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.appendChild(textonodo);
colsfila.setAttribute('style','text-align :rigth;');
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
textonodo=document.createTextNode(vProd);
idnodo='vProd'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('align', 'rigth');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
imgnodo=document.createElement('img');
idnodo='addrow'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addRowdetCompra(this)');
imgnodo.setAttribute('src','../../../imagens/addrow/addrow.png');
colsfila.appendChild(imgnodo);
novafila.appendChild(colsfila);
document.getElementsByTagName('table')[0].appendChild(novafila);
}
}
tablerows=document.getElementsByTagName('table')[0].rows.length;
NUM_FILAS++;
j++;
}
comandosfilas();
}
My code add one row of a table dinamicaly. The alignment wrks for the firs row that is static but not for the other rows. Intead of this I have tested the dinamic content with center align attribute and it works.
Here is the part of the code that I am refering, and below the complete function.
colsfila=document.createElement('td');
textonodo=document.createTextNode(qTrib);
idnodo='qTrib'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.setAttribute('style', 'text-align: center;');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
textonodo=document.createTextNode(vUnTrib);
idnodo='vUnTrib'+NUM_FILAS;
var align='rigth';
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.appendChild(textonodo);
colsfila.setAttribute('style','text-align :rigth;');
novafila.appendChild(colsfila);
function importxmlcompra(){
var novostring= new String(document.getElementById('file').value);
if (novostring!=''){
oPed.open("GET",novostring, false);
oPed.send();
var result = oPed.responseXML;
if (!result.documentElement && oPed.responseStream) {
result.load(oPed.responseStream);
}
}else{
alert ("Por favor seleccione un arquivo XML");
}
if (!result||result.getElementsByTagName('emit').length==0){
return};
var emit=result.getElementsByTagName("emit")[0];
var dEmi=result.getElementsByTagName('dEmi')[0].childNodes[0].nodeValue;
dEmi=dataNF(dEmi);
document.getElementById('dEmi').value=dEmi;
var CNPJ=emit.getElementsByTagName('CNPJ')[0].childNodes[0].nodeValue;
document.getElementById('cnpj').value=CNPJ;
var razaosocialimportada=emit.getElementsByTagName("xNome")[0].childNodes[0].nodeValue;
document.getElementById("xNome").value=razaosocialimportada;
var ide=result.getElementsByTagName("ide")[0];
var nNF=ide.getElementsByTagName("nNF")[0].childNodes[0].nodeValue;
document.getElementById("nNF").value=nNF;
var vNF='R
+number_format(result.getElementsByTagName("vNF")[0].childNodes[0].nodeValue,2,',','.');
document.getElementById("vNF").value=vNF;
var detlength=result.getElementsByTagName("det").length;
var j=0, cProd, xProd, vUnTrib, qTrib, vProd;
var tabledet=document.getElementsByTagName('table')[0];
var tablerows=document.getElementsByTagName('table')[0].rows.length;
var novafila, colsfila, idnodo, textonodo, imgnodo, attid, ant;
while(j<detlength){
det=result.getElementsByTagName('det')[j];
cProd=det.getElementsByTagName('cProd')[0].childNodes[0].nodeValue;
xProd=det.getElementsByTagName('xProd')[0].childNodes[0].nodeValue;
vUnTrib=number_format(det.getElementsByTagName('vUnTrib')[0].childNodes[0].nodeValue,2,',','.');
qTrib=number_format(det.getElementsByTagName('qTrib')[0].childNodes[0].nodeValue,0,'','.');
vProd=number_format(det.getElementsByTagName('vProd')[0].childNodes[0].nodeValue,2,',','.');
if(validacProd(cProd)==true){
if (tablerows==2&& document.getElementById('cProd0').innerHTML=='0'){
document.getElementById('cProd0').innerHTML=cProd;
document.getElementById('xProd0').innerHTML=xProd;
document.getElementById('vUnTrib0').innerHTML=vUnTrib;
document.getElementById('qTrib0').innerHTML=qTrib;
document.getElementById('vProd0').innerHTML=vProd;
}else{
novafila=document.createElement('tr');
textonodo=document.createTextNode(cProd);
attid=document.createAttribute('id');
colsfila=document.createElement('td');
idnodo='cProd'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
tabledet.appendChild(novafila)
colsfila=document.createElement('td');
textonodo=document.createTextNode(xProd);
idnodo='xProd'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
idnodo='codproduto'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('style','display:none');
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
idnodo='descproduto'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
textonodo=document.createTextNode(qTrib);
idnodo='qTrib'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.setAttribute('style', 'text-align: center;');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
textonodo=document.createTextNode(vUnTrib);
idnodo='vUnTrib'+NUM_FILAS;
var align='rigth';
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addInputdetCompra(this)');
colsfila.appendChild(textonodo);
colsfila.setAttribute('style','text-align :rigth;');
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
textonodo=document.createTextNode(vProd);
idnodo='vProd'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('align', 'rigth');
colsfila.appendChild(textonodo);
novafila.appendChild(colsfila);
colsfila=document.createElement('td');
imgnodo=document.createElement('img');
idnodo='addrow'+NUM_FILAS;
colsfila.setAttribute('id', idnodo);
colsfila.setAttribute('onclick','addRowdetCompra(this)');
imgnodo.setAttribute('src','../../../imagens/addrow/addrow.png');
colsfila.appendChild(imgnodo);
novafila.appendChild(colsfila);
document.getElementsByTagName('table')[0].appendChild(novafila);
}
}
tablerows=document.getElementsByTagName('table')[0].rows.length;
NUM_FILAS++;
j++;
}
comandosfilas();
}
您在多个地方拼写正确错误。搜索并将
rightth
替换为right
。You're spelling right incorrectly in multiple places. Do a search and replace for
rigth
toright
.