简体   繁体   English

Javascript-找到TD宽度=“ 70%”并添加CSS

[英]Javascript - Locate TD width=“70%” and add CSS

Is it possible to locate a TABLE with a TD that has a width of 70%, then add additional CSS using plain Javascript? 是否可以找到TD宽度为70%的表,然后使用纯Javascript添加其他CSS?

The TABLE doesnt have an ID, but its nested within a DIV with an ID. TABLE没有ID,但是嵌套在具有ID的DIV中。

HTML: HTML:

<div id="MainContent">
  <table width="100%">
    <tr>  </tr>
    <tr>  </tr>
    <tr>
      <td>
        <table width="100%">
          <tr>
            <td width="70%">
              ....
            </td>
          </tr>
         </table>
       </td>
     </tr>
.....

So #MainContent table THIRD-TR FIRST-TD TABLE TR TD 因此,#MainContent表THIRD-TR FIRST-TD TABLE TR TD

Any ideas and help appreciated! 任何想法和帮助表示赞赏!

Can you use jQuery? 可以使用jQuery吗?

$('td[width=100%]')

If not, this might be a function worth playing with: 如果不是,那么这可能是一个值得玩的功能:

http://snipplr.com/view/1853/get-elements-by-attribute/ http://snipplr.com/view/1853/get-elements-by-attribute/

Good luck. 祝好运。

Which table are you trying to get? 您想拿到哪张桌子? This should get the first one: 这应该是第一个:

document.getElementById('MainContent').getElementsByTagName('table')[0];

But this should get the second one: 但这应该得到第二个:

var tds = document.getElementsByTagName('td'),
i = tds.length;
while (i) {
    i -= 1;
    if (tds[i].width === '70%') {
        tds = tds[i];
        break;
    }
}
do {
    tds = tds.parentNode;
} while (tds.nodeName !== 'TABLE')

At the end of all this, tds should be the first table that is the parent of that 70% td. 最后, tds应该是该70%td的父表的第一个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM