开发者

Get text's tags under this

开发者 https://www.devze.com 2023-02-12 17:35 出处:网络
I have this code JavaScript: var J = jQuery.noConflict(); J(\'#example tr\').click( function() { //HERE THE CODE I WANT

I have this code

JavaScript:

var J = jQuery.noConflict();
J('#example tr').click( function() {
  //HERE THE CODE I WANT
} );

HTML:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
        <thead>
            <tr>
                <th>Immat</th>
                <th>Marque</th>
                <th>Modèle</th>
                <th>Contrat</th>
                <th>Début Contrat</th>
                <th>Fin Contrat</th>
                <th>CoûtHT/mois</th>
</tr>


<tr class="odd gradeA">
                <td>2257YY64</td>
                <td>Citroen</td>
                <td>C3</td>
                <td>Star Lease</td>
                <td>27/04/2009</td>
                <td>27/04/2010</td>
                <td>270,02</td>
            </tr>
            <tr class="odd gradeA">
         开发者_如何学编程       <td>2257YY64</td>
                <td>Citroen</td>
                <td>C3</td>
                <td>Star Lease</td>
                <td>27/04/2009</td>
                <td>27/04/2010</td>
                <td>270,02</td>
            </tr>
            <tr class="odd gradeA">
                <td>2257YY64</td>
                <td>Citroen</td>
                <td>C3</td>
                <td>Star Lease</td>
                <td>27/04/2009</td>
                <td>27/04/2010</td>
                <td>270,02</td>
            </tr>

In a loop i want to get the text ( text() method ) of each td tag under the tr tab that user had clicked.

How can i do this ?


Use .find and .eachh to solve your problem.

J('#example tr').click( function() {
  J(this).find("td").each(function() {
      alert(J(this).text()); // or whatever else you want to do with the text
  });
});


J('#example tr').click( function() {
  var text="";
  J(this).children("td").each(function(){
     text += J(this).text();
  });
  alert(text);
});

Also noticed in your code the <THEAD> tag is not closed. This code may not work, if you leave that open.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号