te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - jquery select nested div - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery select nested div - Stack Overflow

programmeradmin3浏览0评论

How do I select div that contains "my content"?

<table>
  <tr>
    <td class="ms-disc-bordered-noleft">
      <div class="">
        <div>some content</div>
        <div>my content</div>
      </div>
    </td>
  </tr>
</table>

what relation does that div has with td.ms-disc-bordered-noleft?

How do I select div that contains "my content"?

<table>
  <tr>
    <td class="ms-disc-bordered-noleft">
      <div class="">
        <div>some content</div>
        <div>my content</div>
      </div>
    </td>
  </tr>
</table>

what relation does that div has with td.ms-disc-bordered-noleft?

Share Improve this question edited Jan 10, 2013 at 21:56 Athapali asked Jan 10, 2013 at 21:54 AthapaliAthapali 1,0894 gold badges25 silver badges48 bronze badges 4
  • first div has empty class, two divs that contain the actual content have no divs-- these divs are dynamically created.. – Athapali Commented Jan 10, 2013 at 21:58
  • Is <div class=""> important in your tricky question? – Roko C. Buljan Commented Jan 10, 2013 at 22:01
  • I have no control over the markup or i would make it more elegant.. – Athapali Commented Jan 10, 2013 at 22:03
  • Sarika, you have to really explain, do you need the last element , or containing exactly the text my content. your question is so hard to decipher – Roko C. Buljan Commented Jan 10, 2013 at 22:08
Add a ment  | 

6 Answers 6

Reset to default 5
  • $('.ms-disc-bordered-noleft div:last')
  • $('.ms-disc-bordered-noleft div:eq(2)')
  • $('.ms-disc-bordered-noleft').find('div:eq(2)');
  • or $("div:contains('my content'):last");

return the droid...I mean div...you are looking for.

I have a feeling that $('.ms-disc-bordered-noleft div:last') is your best option; my performance test shows that it is generally the fastest of the 4 proposals (FireFox prefers $('.ms-disc-bordered-noleft').find('div:eq(2)');).

See http://jsfiddle/jhfrench/cfeZU for examples of the different selectors in use.


As for the second part of your question, that div is a 'descendant' of the td.ms-disc-bordered-noleft element. More specifically, it is the child of the td's child.

One option as second child of inner <div>:

var div = $(".ms-disc-bordered-noleft > div > div:eq(1)");

It's a parent node, you can traverse up an down using .parent() and .children() or .find()

How do I select div that contains "my content"?

$('.ms-disc-bordered-noleft').find('div').contains("my content");

IF I did not understand well your Q. ...you can use:

$('.ms-disc-bordered-noleft div').find('div:last');

        ^^-parent         ^^-first children  ^^-last div

Most direct route would be:

var div = $("div:contains('my content'):last");

As far as the relationship to the td goes, you could start with the above and work your way back.

var cell = div.closest('td');

These aren't necessarily the fastest options in terms of performance, but they are the shortest code. And I like short code.

.ms-disc-bordered-noleft div div:nth-child(2)

or

.ms-disc-bordered-noleft div div:last-child
发布评论

评论列表(0)

  1. 暂无评论