javascript - how to get span in tr with jquery - Stack Overflow

I have a table:<table id="myTable"><tbody><tr><td><span data-mydat

I have a table:

<table id="myTable">
    <tbody>
      <tr>
       <td><span data-mydata="data1">first value data1</span></td>
       <td><span data-mydata="data2">first value data2</span></td>
      </tr>
       <tr>
       <td><span data-mydata="data1">second value data1</span></td>
       <td><span data-mydata="data2">second value data2</span></td>
      </tr>
   </tbody>
 </table>

I want to change the span texts at the last tr with jquery, I got all the span in the row with:

$('#myTable tbody tr:last td span')

what I want is the span which attr data-mydata is equal to data2.

how can I do it?

I have a table:

<table id="myTable">
    <tbody>
      <tr>
       <td><span data-mydata="data1">first value data1</span></td>
       <td><span data-mydata="data2">first value data2</span></td>
      </tr>
       <tr>
       <td><span data-mydata="data1">second value data1</span></td>
       <td><span data-mydata="data2">second value data2</span></td>
      </tr>
   </tbody>
 </table>

I want to change the span texts at the last tr with jquery, I got all the span in the row with:

$('#myTable tbody tr:last td span')

what I want is the span which attr data-mydata is equal to data2.

how can I do it?

Share Improve this question asked Jan 23, 2017 at 17:09 cucurucucuru 3,72811 gold badges47 silver badges86 bronze badges 2
  • jQuery Learning Center: link Selecting Elements by Attribute – Andreas Commented Jan 23, 2017 at 17:12
  • Do you want to set value of span attr or select the span with attr? Answer is relative to that. – Rahi Commented Jan 23, 2017 at 17:16
Add a ment  | 

5 Answers 5

Reset to default 2

Use attribute equals selector to select the element based on the attribute value.

 $('#myTable tbody tr:last td span[data-mydata="data2"]')

Use :nth-child instead of jQuery :last for making it more faster.

$('#myTable tbody tr:last-child td span[data-mydata="data2"]')

You can try the following:

$('#myTable tbody tr:last span[data-mydata="data2"]').text('to whatever text you want.');

Use

$('#myTable').find('span[data-mydata="data2"]').text();

Use the last selector to get the last tr then use [attribute=value]

$("tr:last span[data-mydata='data2']").css({"background": "gold"})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="myTable">
    <tbody>
      <tr>
       <td><span data-mydata="data1">first value data1</span></td>
       <td><span data-mydata="data2">first value data2</span></td>
      </tr>
       <tr>
       <td><span data-mydata="data1">second value data1</span></td>
       <td><span data-mydata="data2">second value data2</span></td>
      </tr>
   </tbody>
 </table>

Try this:

$('#myTable tbody tr:last td span').attr('data-mydata','data2')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744731953a4590517.html

相关推荐

  • javascript - how to get span in tr with jquery - Stack Overflow

    I have a table:<table id="myTable"><tbody><tr><td><span data-mydat

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信