2014-09-03 55 views
0

HTML:我怎麼能獲得TD數據從CSS獲得附加到我的當前行,並用我的邏輯

<table class="myTable" border="1" width="80%"> 
<thead> 
    <tr> 
     <td></td> 
     <td>one</td> 
     <td><button onClick="MoveUp.call(this);">&#8679;</button></td> 
     <td><button onClick="MoveDown.call(this);">&#8681;</button></td> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
     <td></td> 
     <td>two</td> 
     <td><button onClick="MoveUp.call(this);">&#8679;</button></td> 
     <td><button onClick="MoveDown.call(this);">&#8681;</button></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td>three</td> 
     <td><button onClick="MoveUp.call(this);">&#8679;</button></td> 
     <td><button onClick="MoveDown.call(this);">&#8681;</button></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td>four</td> 
     <td><button onClick="MoveUp.call(this);">&#8679;</button></td> 
     <td><button onClick="MoveDown.call(this);">&#8681;</button></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td>five</td> 
     <td><button onClick="MoveUp.call(this);">&#8679;</button></td> 
     <td><button onClick="MoveDown.call(this);">&#8681;</button></td> 
    </tr> 
</tbody> 

CSS:

table.myTable { 
    counter-reset: rowNumber; 
} 
table.myTable tr { 
    counter-increment: rowNumber; 
} 
table.myTable tr td:first-child: :before { 
    content: counter(rowNumber); 
    min-width: 1em; 
    margin-right: 0.5em; 
} 

javascript:

function get_previoussibling(n) { 
    x = n.previousSibling; 
    while (x.nodeType != 1) { 
     x = x.previousSibling; 
    } 
    return x; 
} 

function get_nextsibling(n) { 
    x = n.nextSibling; 
    while (x != null && x.nodeType != 1) { 
     x = x.nextSibling; 
    } 
    return x; 
} 

function MoveUp() { 
    var table, 
     row = this.parentNode; 

    while (row != null) { 
     if (row.nodeName == 'TR') { 
      break; 
     } 
     row = row.parentNode; 
    } 
    table = row.parentNode; 
    table.insertBefore(row, get_previoussibling(row)); 
} 

function MoveDown() { 
    var table, 
     row = this.parentNode; 

    while (row != null) { 
     if (row.nodeName == 'TR') { 
      break; 
     } 
     row = row.parentNode; 
    } 
    table = row.parentNode; 
    table.insertBefore(row, get_nextsibling(get_nextsibling(row))); 
} 

FIDDLE:http://jsfiddle.net/z5hroz4p/10/

HTML表格第一款TD值是從CSS。如何以獲取該值在我prgramming

小提琴使用讓你更清楚。

在此先感謝

+3

那麼,你的問題並不清楚。 – 2014-09-03 06:19:36

+0

user3383271已經在您之前的帖子中回答了您的問題。這不行嗎?你只想在CSS中做到這一點? – 2014-09-03 06:36:31

+0

不,它不工作,你可以張貼我的小提琴 – Karthika 2014-09-03 06:38:11

回答

1

不幸的是,這幾乎是不可能的,只要在這樣的方式產生的內容做只存在於Web文檔的佈局。 已經有同樣問題的相似問題,不幸的是,他們沒有解決方案。 你可以在這裏看到相關的主題:How to access CSS generated content with JavaScript

在這種情況下,我建議你使用JavaScript創建和編號的行,但不是CSS。