最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Align cell javascript - Stack Overflow

programmeradmin2浏览0评论

i'm using javascript to add rows to a table dynamically:

function addRow() {
    var table = document.getElementById("bestelling");

    var rowCount = table.rows.length; 
    var row = table.insertRow(rowCount-5);

    var cell1 = row.insertCell(0);
    var bedrag = document.createElement("input");
    bedrag.type = "text";
    bedrag.name = "bedrag[]";
    cell1.appendChild(bedrag);
}

This seems to work perfectly, except I want the first cell to align right.

Any suggestions?

i'm using javascript to add rows to a table dynamically:

function addRow() {
    var table = document.getElementById("bestelling");

    var rowCount = table.rows.length; 
    var row = table.insertRow(rowCount-5);

    var cell1 = row.insertCell(0);
    var bedrag = document.createElement("input");
    bedrag.type = "text";
    bedrag.name = "bedrag[]";
    cell1.appendChild(bedrag);
}

This seems to work perfectly, except I want the first cell to align right.

Any suggestions?

Share Improve this question asked Jan 4, 2014 at 13:59 crodiecrodie 1121 gold badge1 silver badge10 bronze badges 1
  • can you create a fiddle at jsfiddle – Zword Commented Jan 4, 2014 at 14:10
Add a ment  | 

2 Answers 2

Reset to default 7

You can try adding this (I am assuming that cell1 is the "first cell" that you are referring to):

cell1.style.textAlign = "right";

This styles the cell with CSS textAlign right.


Alternatively, you can set a class name to the cell with this JavaScript code:

cell1.className = "alignRight"

And use CSS to set the align right.

.alignRight{
     text-align:right;
}

To make this even simpler, you could use jQuery - http://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js

$("<tr id='new-row'>").insertBefore("#first-row")      // # is the id selector

Then you can set the style and content of #new-row using the html() and css() methods:

$("#new-row").css("text-align","right");
$("#new-row").html("Hello world!");

Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论