根据HTML元素内容(使用jQuery)移动HTML元素的位置?

时间:2011-12-04 04:02:40

标签: jquery

我有这张桌子:

 <table class="wpjb_job_info">
    <tbody>
      <tr>
        <td><b>Company Name</b></td>

        <td>English First Tianjin</td>
      </tr>

      <tr>
        <td><b>Location</b></td>

        <td>Tianjin, China</td>
      </tr>

      <tr>
        <td><b>Date Posted</b></td>

        <td>Nov 20, 2011</td>
      </tr>

      <tr>
        <td><b>Category</b></td>

        <td><a href=
        "http://goldstarteachers.com/esl-jobs-in-china/category/eslteacher/">ESL
        Teacher</a></td>
      </tr>

      <tr>
        <td><b>Job Type</b></td>

        <td><a href=
        "http://goldstarteachers.com/esl-jobs-in-china/type/full-time/">Full-Time</a></td>
      </tr>

      <tr>
        <td><b>Students</b></td>

        <td>Young Learners</td>
      </tr>

      <tr>
        <td><b>Salary</b></td>

        <td>7,500 - 10,000 RMB / month</td>
      </tr>

      <tr>
        <td><b>Interviewing</b></td>

        <td>Yes</td>
      </tr>

      <tr>
        <td><b>Contract Length</b></td>

        <td>12 months</td>
      </tr>

      <tr>
        <td><b>Accommodation</b></td>

        <td>Provided</td>
      </tr>

      <tr>
        <td><b>Flight Allowance</b></td>

        <td>9,000 RMB</td>
      </tr>

      <tr>
        <td><b>Holidays</b></td>

        <td>12 days public + 10 days personal</td>
      </tr>

      <tr>
        <td><b>Insurance</b></td>

        <td>12 months worldwide ERIKA travel, property and health insurance</td>
      </tr>

      <tr>
        <td><b>Teacher Training</b></td>

        <td>Provided</td>
      </tr>

      <tr>
        <td><b>Work Visa</b></td>

        <td>Legal Z work visa provided</td>
      </tr>
    </tbody>
  </table>

例如,我想这样做:

  <tr>
    <td><b>Salary</b></td>

    <td>7,500 - 10,000 RMB / month</td>
  </tr>

  <tr>
    <td><b>Interviewing</b></td>

    <td>Yes</td>
  </tr>

看起来像这样:

      <tr>
        <td><b>Interviewing</b></td>

        <td>Yes</td>
      </tr>

      <tr>
        <td><b>Salary</b></td>

        <td>7,500 - 10,000 RMB / month</td>
      </tr>

使用jQuery。换句话说,告诉jQuery:移动具有采访文本的tr,并将其移到具有薪资内容的tr之上。怎么做到这一点?

2 个答案:

答案 0 :(得分:1)

像这样:

var interviewing = $('tr > td > b:contains(Interviewing)').closest('tr');
interviewing.insertBefore( interviewing.prev() );

或者像这样:

var interviewing = $('tr > td > b:contains(Interviewing)').closest('tr');
interviewing.prev().before( interviewing );

答案 1 :(得分:1)

我没有对此进行过测试,但以下情况应该可行,

$('tr:contains("Interviewing")').before( $('tr:contains("Salary")') )

或更准确地说,

$('.wpjb_job_info tr:contains("Interviewing")').before( $('.wpjb_job_info tr:contains("Salary")') )