在 jQuery 中,如何指定仅在特定区域执行某个功能?
我有这个 jQuery 片段:
$(function() {
$("ul > li").hover(function() {
$(this).find("ul").stop(true, true).animate({opacity: 'toggle'}, 'fast');
});
});
这将打开另一个
中的每个
内容,但我只想将此用于 :EDIT
这是结构,我修改了一下,本来我想将它添加到我的header id
但我认为这样更好:
<ul id="navigation">
<li>List item 1
<ul>
<li>Sub List Item 1</li>
<li>Sub List Item 2</li>
<li>Sub List Item 3</li>
</ul>
</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
如果问题很愚蠢,请不要吃我,我真的是 jQuery 的新手:)
I have this jQuery snippet:
$(function() {
$("ul > li").hover(function() {
$(this).find("ul").stop(true, true).animate({opacity: 'toggle'}, 'fast');
});
});
This opens every <ul>
what's inside in another <ul>
, but I want to make this only for
:
<div id="header"></div>
EDIT
Here is the structure, I modified it a bit, originally I want to add it to my header id
but that's better I think:
<ul id="navigation">
<li>List item 1
<ul>
<li>Sub List Item 1</li>
<li>Sub List Item 2</li>
<li>Sub List Item 3</li>
</ul>
</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
Please don't eat me if the question is stupid I'm really really new in jQuery :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设
#navigation
div 中只有一个ul
:上面使用的选择器仅选择作为 ul 直接后代的
li
元素它本身就是id="navigation"
元素的后代。您还可以简单地将 id 应用于
ul
,例如id="navList"
,然后使用:Edited following OP's changed question/posted (x)html.
给出以下标记:
并假设他希望用户将鼠标悬停在
li
元素上以显示li
的子ul
:请注意我还没有测试您尝试应用的功能,我相信您已经可以使用它了。
Assuming that there's only one
ul
within the#navigation
div:The selector used, above, selects only
li
elements that are direct descendants of a ul that is itself a descendant of an element ofid="navigation"
.You could, also, simply apply an id to the
ul
, for exampleid="navList"
and then use:Edited following OP's changed question/posted (x)html.
Given the following mark up:
And assuming that he wants the user to hover over the
li
elements to reveal thatli
s childul
:Please note that I haven't tested the function you're trying to apply, I take it on trust that you've already got it working.
只需更改
为
即可限制
header
div 内的元素。Just change
To
This is going to limit the elements for the ones inside the
header
div.David 是正确的,或者将类或 ID 添加到您的
中并直接定位它:
$('ul#ul-id').hover( /* .... */ );
David is correct, or add a class or ID to your
<UL>
and target it directly:$('ul#ul-id').hover( /* .... */ );
BrunoLM 提供了一个很好的答案,只需将 jQuery 选择器更改为
#header ul >李
。 jQuery 理解 CSS。但在 jQuery 中需要了解的一件好事是,您可以提供上下文 用于您的 jQuery 选择器。通常 jQuery 选择器会查找整个 DOM。如果您想缩小范围或让它在其他地方查找,请提供上下文。要在
context
中查找this
,请使用以下形式:在您的具体情况下,这将是:
现在,上下文通过内部应用
.find()
,所以上面的意思是:上面的两个方法是如果您无法应用 CSS 来解决问题,这非常有用...假设您正在动态创建一个 DOM 元素,并且您想在该元素中使用 jQuery。
BrunoLM provides a good answer, just change the jQuery selector to
#header ul > li
. jQuery understands CSS.But a good thing to know about in jQuery is that you can provide a context for your jQuery selector. Normally a jQuery selector looks in the entire DOM. If you want to narrow this or have it look somewhere else provide a context. To look for
this
in acontext
use the form:In your specific case this would be:
Now, the context works by internally applying
.find()
, so the above is synonymous to:The above two methods are very useful if you can't apply CSS to the problem... Let's say you're creating a DOM element on the fly, and you want to use jQuery within that element.