如何使用 jQuery 创建通用 id 引用
我有大约 6 个具有相同类的 div 元素。当我将鼠标悬停在其中任何一个上时,我想在它们旁边显示另一个 div。
我正在考虑给它们全部一个 id="mydiv-divname" 形式的 id,其中 mydiv- 将始终保持不变。
我将如何引用 mydiv-* 元素。我找不到确切的语法,但我认为它应该类似于 $("#mydiv-"[*]),其中 * 代表某种通配符。
I have about 6 div elements with the same class. When I mouseover any one of them I want to show a nother div next to them.
I am thinking of giving them all an id of the form id="mydiv-divname" where mydiv- will always be constant.
How would I reference the mydiv-* elements. I can't find the exact syntax but I think it should be something like $("#mydiv-"[*]) where the * represents some kind of wildcard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ID 有什么用途?如果它们都用相同的类名标记,您可以按类访问它们:
要在悬停其中一个元素时触发事件,请使用
请注意,hover() 中的函数只会针对悬停的元素触发实际上正在悬停。
他们所做的事情与您想要实现的目标类似 here - 悬停时淡入或淡出一个元素(页面上标有该类的许多元素)
What purpose does the ID serve? If they are all tagged with the same class name, you can access them all by class:
To trigger an event when one of those elements is hovered, use
Note that the function within the hover() will only be triggered for the element which is actually being hovered.
They do something similar to what you're trying to achieve here - fading one element in or out on hover (of many elements on the page marked with that class)
为什么不能只在选择器中使用类而不是 id,如
jQuery('.commonClass');
Why can't you just use the class in the selector instead of the id, as in
jQuery('.commonClass');
看起来你想要这样的东西:
HTML:
JAVASCRIPT:
ALTERNATIVE JAVASCRIPT:
It seems you are going for something like this:
HTML:
JAVASCRIPT:
ALTERNATIVE JAVASCRIPT: