html - In javascript, how to get a class name from a td cell? - Stack Overflow

In javascript, how to get a class name from a td cell?example: <td class="ColumnHeader" st

In javascript, how to get a class name from a td cell?

example:

<td class="ColumnHeader" style="text-align:right;" >

class "ColumnHeader" is a class inside css, how could i retrieve it from css and changed the width size in javascript?

In javascript, how to get a class name from a td cell?

example:

<td class="ColumnHeader" style="text-align:right;" >

class "ColumnHeader" is a class inside css, how could i retrieve it from css and changed the width size in javascript?

Share Improve this question edited Nov 15, 2011 at 2:35 Michael Berkowski 271k47 gold badges450 silver badges393 bronze badges asked Nov 15, 2011 at 2:32 C PWLC PWL 5196 gold badges10 silver badges26 bronze badges 1
  • Are you trying to modify a stylesheet with JS or modify the class attribute of a DomNode? – fnp Commented Nov 15, 2011 at 2:36
Add a ment  | 

2 Answers 2

Reset to default 7

Select all the <td> elements via getElementsByTagName() and iterate over them looking for the className:

var tds = document.getElementsByTagName("td");
for (var i = 0; i<tds.length; i++) {

  // If it currently has the ColumnHeader class...
  if (tds[i].className == "ColumnHeader") {
    // Set a new width
    tds[i].style.width = new_width;

    // Or set a different class which defines the width
    tds[i].className = "someOtherClass";
  }
}

You can't really change the width of the css class programatically, but you can change it on the element:

td.style.width = newWidth;

To get the class name from an element, use:

var className = td.className;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1740274983a4220353.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信