2017-09-13 3 views
-1

각 th 내부에 일부를 추가하려고합니다. 표 헤더 생성 된 동적 여기에 내가 가지고있는 데이터베이스 열추가 일부 JQuery를 사용하여 각 테이블 헤더 (th)에 일부 텍스트 추가

에 의존한다 :

<table id="dynamic_id" class="table table-striped table-bordered"> 
    <thead> 
     <tr> 
      <th>Company Name</th> 
      <th>Email</th> 
      <th>Mobile</th> 
      <th>Address</th> 
     </tr> 

    </thead> 
    <tbody> 
     //body goes here 
    </tbody> 

    </tbody> 
</table> 

는 준비 이벤트에 jQuery를 사용하여을 기대하는 것입니다.

<th>Company Name 
      <span class="js-sorter-desc fa fa-chevron-down pull-right"></span> 
      <span class="js-sorter-asc fa fa-chevron-up pull-right"></span> 
</th> 

이 각 목에 적용해야한다 ..

<span class="js-sorter-desc fa fa-chevron-down pull-right"></span> 
<span class="js-sorter-asc fa fa-chevron-up pull-right"> 
+0

http://api.jquery.com/append 스택 오버플로 –

+0

에 오신 것을 환영합니다! [둘러보기]를 가지고 둘러보고 [도움], 특히 [* 좋은 질문을하는 법 *] (/ help/how-to-ask)을 읽으십시오. [jQuery API] (http://api.jquery.com)를 통해 작업을 수행하십시오. ** ** 특정 문제가 생기면 현장에서보다 철저한 조사와 검색 (/ 도움/검색)을 한 후 해결할 수없는 경우 문제를 함께 게시하십시오 그것으로. 사람들이 기꺼이 도와 드리겠습니다. –

+0

테이블이 동적 인 경우 사용하는 열 수를 계산하고 각 테이블을 사용하여 내부에 원하는 것을 추가하십시오 –

답변

1

당신은 appendJQuery와 방법을 사용해야합니다.

또한 thDOM 요소를 얻으려면 find 메서드를 사용하십시오.

$('.table.table-striped').find('thead tr th').append('<span class="js-sorter-desc fa fa-chevron-down pull-right"></span><span class="js-sorter-asc fa fa-chevron-up pull-right"></span>'); 
 
console.log($('.table.table-striped').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="dynamic_id" class="table table-striped table-bordered"> 
 
    <thead> 
 
     <tr> 
 
      <th>Company Name</th> 
 
      <th>Email</th> 
 
      <th>Mobile</th> 
 
      <th>Address</th> 
 
     </tr> 
 

 
    </thead> 
 
    <tbody> 
 
     //body goes here 
 
    </tbody> 
 
</table>

+0

여기서'each()'는 필요 없습니다. –

+0

@RoryMcCrossan, 고맙습니다! 업데이트 됨. –

0

이 시도 :

$(document).ready(function(){ 
    $('table').find('th')append('<span class="js-sorter-desc fa fa-chevron-down pull-right"></span><span class="js-sorter-asc fa fa-chevron-up pull-right">'); 
}); 
+0

여기서'each()'는 필요 없습니다. 또한 요점은'id'가 런타임에 동적으로 생성되므로, 그것을 선택하는 것이 아마도 효과가 없을 것입니다. –

+0

@RoryMcCrossan, 고맙습니다. 제 답변을 업데이트했습니다. 동적 코드를 사용하기 위해'each '를 사용했습니다. 나는 그가 모든 ''에 추가하기를 원한다는 것을 안다. 그래서 우리는 이것을 단순히 선택자로 간주 할 수있다. –

+0

그게 무슨 뜻인지는 모르지만 여전히'each()' 'dynamic'이 필요하지 않습니까? –