javascript - jquery-how to detect child id? - Stack Overflow

<div id="first"><div id="here">...<div><div><div id=

<div id="first">
    <div id="here">...</div>
</div>
<div id="second">
    <div id="here">...</div>
</div>

jquery:

$("#second #here").click(function(){});

how to write jquery to detect when I click the second ?

<div id="first">
    <div id="here">...</div>
</div>
<div id="second">
    <div id="here">...</div>
</div>

jquery:

$("#second #here").click(function(){});

how to write jquery to detect when I click the second ?

Share Improve this question asked Jul 23, 2009 at 2:12 skargorskargor 1,0313 gold badges15 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

This is the wrong question to be asking, because you are not supposed to have duplicate IDs in a document. An ID is like the social security number of an element. You can't give multiple elements the same one, because then when you tell Javascript to find an element by ID it will be terribly confused by the fact there's more than one and give you unexpected results. The reason ID lookups are as fast as they are is because the browser can have a hash table of ID->element - violating that understanding is a bad practice, to say the least.

When you have several elements that are all of the same "type", the proper practice is to class them:

<div id="first">
    <div class="here">...</div>
</div>
<div id="second">
    <div class="here">...</div>
</div>

So then you can do:

$('#first').find('div.here');

Or:

$('div.here', '#second');

Or:

$('#first div.here');

Which would all return what you expect them to return.

This is what you are looking for, but like Paolo said, you cannot have duplicate ID's. If you're styling things, use a class.

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

相关推荐

  • javascript - jquery-how to detect child id? - Stack Overflow

    <div id="first"><div id="here">...<div><div><div id=

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>