最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to select first td element and its text with Jquery - Stack Overflow

programmeradmin10浏览0评论

I want to change the "Yes! Pick me" into "Picked" with Jquery in the following HTML structure, I used $('#myDiv>table>tr>td>table>tr').eq(1).text("Picked"); But it was not working. Could someone shed some light on this please? Thanks!

FYI, the first td of the the first table itself contains another table...

 <div id="myDiv">
    <table>
      <tr>
        <td>
          <table>
            <tr>
              <td>Yes! Pick me!</td>

              <td>Not me..</td>
            </tr>

            <tr>
              <td>Not me..</td>
            </tr>
          </table>
        </td>

        <td>Not me..</td>
      </tr>

      <tr>
        <td>Not me..</td>
      </tr>
    </table>
  </div>

The section $('#myDiv>table>tr>td>table>tr>td').eq(1).text("Picked"); does the trick, I forgot the last td part. Thanks to Rocket and everyone's help.

I want to change the "Yes! Pick me" into "Picked" with Jquery in the following HTML structure, I used $('#myDiv>table>tr>td>table>tr').eq(1).text("Picked"); But it was not working. Could someone shed some light on this please? Thanks!

FYI, the first td of the the first table itself contains another table...

 <div id="myDiv">
    <table>
      <tr>
        <td>
          <table>
            <tr>
              <td>Yes! Pick me!</td>

              <td>Not me..</td>
            </tr>

            <tr>
              <td>Not me..</td>
            </tr>
          </table>
        </td>

        <td>Not me..</td>
      </tr>

      <tr>
        <td>Not me..</td>
      </tr>
    </table>
  </div>

The section $('#myDiv>table>tr>td>table>tr>td').eq(1).text("Picked"); does the trick, I forgot the last td part. Thanks to Rocket and everyone's help.

Share Improve this question edited Oct 27, 2021 at 1:48 ashleedawg 21.6k9 gold badges81 silver badges116 bronze badges asked Feb 24, 2012 at 20:57 eastboundreastboundr 1,8778 gold badges28 silver badges46 bronze badges 3
  • 1 why don't you give ids for the fields you want to select? – Neysor Commented Feb 24, 2012 at 20:59
  • Hi Neysor, b/c that part of the code is auto generated by an ASP user control. I'm just posting a sample code here to show similar structure for easier reference. thx – eastboundr Commented Feb 24, 2012 at 21:02
  • Hi, Rocket, worked like a charm! Thanks a Ton!!! Sloppy me~ – eastboundr Commented Feb 24, 2012 at 21:05
Add a ment  | 

6 Answers 6

Reset to default 9

Try this:

$("#myDiv table table td:first").text("Picked")
$('#myDiv').find('table table td').eq(0).text(...);

Start your selection at the #myDiv element ($('#myDiv')), then find all the TD element that are inside a table that is inside another table (.find('table table td')), then only alter the first one (.eq(0)).

Documentation:

  • .find(): http://api.jquery./find
  • .eq(): http://api.jquery./eq

The main problem is that you want .eq(0) not .eq(1) as .eq() is 0-based, and you are not selecting the td, only the tr.

Other than that using > direct descendant selectors makes your selection not very robust at all.

Try $('#myDiv table table td').eq(0).text('Picked');

You can try:

$("td:contains('Yes! Pick me!')").text("Picked"); ​

You can use the :contains(text) selector

$('#myDiv td table td:contains(Yes! Pick me!)').text('Picked');

Be careful with nested tables however because if you were to use just

$('#myDiv td:contains(Yes! Pick me!)').text('Picked');

You would get both the cell your after plus the cell it is nested within.

Your child selector query won't work because HTML5 requires the parser to insert <tbody> elements inside your <table> elements, since you've forgotten to put them in yourself. Perhaps you should consider validating your HTML?

发布评论

评论列表(0)

  1. 暂无评论