2016-08-05 3 views
-2

SampleImage자바 스크립트 : 현재 셀 요소 위에 셀 bgcolor를 가져 오는 방법은 무엇입니까?

<tr> 
    <td></td> 
    <td bgcolor = "Brown"></td> 
    <td></td> 
</tr> 

<tr> 
    <td></td> 
    <td id="current"></td> 
    <td></td> 
</tr> 

어떻게 다음 BGCOLOR 브라운 같은 경우는 비교 선택된 셀에 참조로 셀의 ID를 사용하여 셀 위의 셀의 BGCOLOR를 얻기 위해 자바 스크립트를 사용합니까?

편집 :

function move2(barName, start, currentCell, totSec,barLength) { 
    var no = start; 
    var current = currentCell; 
    var totalSec = totSec; 
    var span = no - current; 
    var bar = barName + no; 
    var lengthCount = 0; 
    var progLength = barLength - 1; 
    var elem = document.getElementById(bar); 
    var width = 0; 
    var stop = 0; 
function fill(){ 
     while(no < current){ 
      if(lengthCount >= progLength){ 
       stop = 1; 
       break; 
      } 
      elem.style.width = '100%'; 



    if ($(this).parent().next().children("elem").index() == "Brown") { 
    elem.className = 'progress-bar progress-bar-danger'; 
    } 


     no++; 
     bar = barName + no; 
     elem = document.getElementById(bar); 
     lengthCount++; 
     stop = 0; 
    } 

    } 

}

죄송합니다, 내 시도는 내가 문제가 좀 비슷하지만 단지를하지 않는 다른 게시물에서 발견 한 몇 가지 jQuery를 코드를 시도하고있다 부모와 자식 메소드. 진행률 표시 줄에서 일하고 있는데, 위의 셀 (시간 행)이 갈색 (중단 시간) 인 경우 진행률 막대가 빨간색으로 바뀌므로 기본 셀의 클래스가 progress-bar-success에서 progress-bar- 위의 세포가 갈색이면 위험합니다.

+2

ID가없고 코드도 없습니다. 당신도 시도한 것을 보여주고, 더 이상 사용되지 않는 TD 속성 (bgcolor)을 사용하고 있습니다. - 그래도 괜찮습니다. 아무도 당신을 위해 시간에 당신을 위해 일하지 않을 것입니다. –

+1

@JaromandaX처럼, 현재 작업을 제공하십시오. – Joundill

답변

0

나는 산세 jQuery를 사용이기는하지만, Sandip 등이 거의 동일한 방법에 대해 갔다. 먼저, 페이지로드가 완료되면 브라우저에 내 함수 호출을 요청했습니다. 다음으로 HTMLCollection 데이터 형식을 확장하여 배열 데이터 형식의 forEach 함수를 HTMLCollection에 사용할 수 있도록합니다.

페이지가 완료되면 코드가 코드를 호출하고 우리가 찾는 셀을 즉시 찾습니다 (t1). 이 작업을 완료하면 테이블의 rows 컬렉션을 통해 t1의 parentNode와 일치하는 테이블을 찾습니다.이 테이블은 해당 테이블을 포함하는 행입니다. 다음으로 우리는 셀 컬렉션을 단계별로 실행하고 t1 셀을 다시 검색합니다. 즉, 우리는 이제 초기 목표 인 t1의 셀 인덱스와 행을 모두 가지고 있음을 의미합니다.

거기에서 우리는 행 인덱스에 1을 빼고 최종 목표 인 t2를 얻습니다. 이 셀이 있으면 우리는 속성 (bgcolor) 값이나 내용 등의 정보를 얻을 수 있습니다.

<!doctype html> 
<html> 
<head> 
<script> 
window.addEventListener('load', onDocLoaded, false); 
function byId(id){return document.getElementById(id);} 
HTMLCollection.prototype.forEach = Array.prototype.forEach; 

function onDocLoaded(evt) 
{ 
    // 1. get id=current cell 
    var curCell = byId('current'); 

    // 2.get current row 
    var curRow = curCell.parentNode; 
    var table = curRow.parentNode.parentNode; // parentNode twice, since we're using both a table and a tbody element to wrap the rows. 

    // 3. get index of current row in table's 'rows' collection 
    var rowIndex = -1; 
    table.rows.forEach(function(elem, index, array){if (elem == curRow)rowIndex = index;}); 

    // 4. get index of current cell in table's current row's 'cells' collection 
    var cellIndex = -1; 
    table.rows[rowIndex].cells.forEach(function(elem, index, array){if (elem == curCell)cellIndex = index;}); 

    // 5. get data from the original target cell - the one below the one with an id of 'current' 
    alert(table.rows[rowIndex-1].cells[cellIndex].getAttribute('bgcolor')); 
    alert(table.rows[rowIndex-1].cells[cellIndex].textContent); 

} 
</script> 
</head> 
<body> 
    <table> 
     <tbody> 
      <tr> 
       <td></td> 
       <td bgcolor = "Brown">blah</td> 
       <td></td> 
      </tr> 

      <tr> 
       <td></td> 
       <td id="current"></td> 
       <td></td> 
      </tr>  
     </tbody> 
    </table> 
</body> 
</html> 
0

가장 가까운 행을 찾습니다 다음 이전 행 찾은 다음 각 루프 모든 세포를 얻을 을 통해 필수 속성을 찾을 수

 $(document).ready(function() {    
     $("#current").closest("tr").prev().find("td").each(function (a, b) { 

      if(String(b["bgColor"]).trim()=="Brown") 
      { 
       //here is cell which has brown bgcolor 
       alert("brown"); 
      } 
     }); 
    }); 
0
당신의 배경 색상을 얻기에 jQuery를 사용해야합니다

그 대상. 이 같은

는 :

var color=$("#tdwithbgcolor").css("background-color"); 
$("#tdtosetcolor").css("background-color",color);