开发者

jQuery class selector -- what scope does it default to?

I have a q开发者_StackOverflow中文版uestion about the class selector in jQuery. I'm looking at a page which uses a jQuery plugin called slidedeck, and the page author has two <div>s showing two different slidedeck settings. Along these lines:

<div id="slidedeck_frame" class="skin-slidedeck"><dl class="slidedeck">
  <!-...HTML in here-->
</div>

<script type="text/javascript">
        $('.slidedeck').slidedeck({
                autoPlay: true,
                cycle: true, 
                autoPlayInterval: 2500, // 2.5 seconds
                hideSpines: true
            });
</script>

<div id="slidedeck_frame" class="skin-slidedeck"><dl class="slidedeck">
  <!-...HTML in here-->
</div>

<script type="text/javascript">
            $('.slidedeck').slidedeck();
</script>

So you have two <div>s sharing the same ids and CSS classes for their children, but with different slidedeck settings.

I would have thought the jQuery class selector would have applied the last slidedeck setting to both <dl>s, but in fact they each use the slidedeck settings directly below them. I must not be understanding the jQuery selector scope (quite likely), or is there something else at play here possibly?


I must not be understanding the jQuery selector scope (quite likely)

You are misunderstanding the concept of IDs. ID attributes are meant to be unique across the elements in DOM. I hope that clears it up :)


Duplicate IDs are not valid in HTML. The behavior is not defined.

EDIT

In this case, as you are not using the ID as a selector, your jquery selector should return both of the tags with the class in the selector.

http://jsfiddle.net/cJ4wp/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜