如何使用 jQuery 创建通用 id 引用

发布于 2024-08-12 19:58:38 字数 206 浏览 4 评论 0原文

我有大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洒一地阳光 2024-08-19 19:58:38

ID 有什么用途?如果它们都用相同的类名标记,您可以按类访问它们:

`$(".className")...

要在悬停其中一个元素时触发事件,请使用

`$(".className").hover(... )

请注意,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:

`$(".className")...

To trigger an event when one of those elements is hovered, use

`$(".className").hover(... )

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)

顾忌 2024-08-19 19:58:38

为什么不能只在选择器中使用类而不是 id,如

jQuery('.commonClass');

Why can't you just use the class in the selector instead of the id, as in

jQuery('.commonClass');

倾`听者〃 2024-08-19 19:58:38

看起来你想要这样的东西:

HTML:

<div class="content" id="con_a">Hello world.</div>
  <div id="show_con_a" style="display:none">Show Me on content div "a" hover</div>

<div class="content" id="con_b">Nice to meet you.</div>
  <div id="show_con_b" style="display:none">Show Me on content div "b" hover</div>

<div class="content" id="con_c">See you later.</div>
  <div id="show_con_c" style="display:none">Show Me content div "c" hover</div>

JAVASCRIPT:

//Collect all divs with 'content' class
$('.content').each(function(){
    //Set mouse handler for each content div
    $(this).hover(function(){
        $('#show_' + this.id).show();
    },
    function(){
        $('#show_' + this.id).hide();
    });
});

ALTERNATIVE JAVASCRIPT:

//Collect all divs with an id that begins with 'con_'
$("[id=^'con_']").each(function(){
    //Set mouse handler for each content div
    $(this).hover(function(){
        $('#show_' + this.id).show();
    },
    function(){
        $('#show_' + this.id).hide();
    });
});

It seems you are going for something like this:

HTML:

<div class="content" id="con_a">Hello world.</div>
  <div id="show_con_a" style="display:none">Show Me on content div "a" hover</div>

<div class="content" id="con_b">Nice to meet you.</div>
  <div id="show_con_b" style="display:none">Show Me on content div "b" hover</div>

<div class="content" id="con_c">See you later.</div>
  <div id="show_con_c" style="display:none">Show Me content div "c" hover</div>

JAVASCRIPT:

//Collect all divs with 'content' class
$('.content').each(function(){
    //Set mouse handler for each content div
    $(this).hover(function(){
        $('#show_' + this.id).show();
    },
    function(){
        $('#show_' + this.id).hide();
    });
});

ALTERNATIVE JAVASCRIPT:

//Collect all divs with an id that begins with 'con_'
$("[id=^'con_']").each(function(){
    //Set mouse handler for each content div
    $(this).hover(function(){
        $('#show_' + this.id).show();
    },
    function(){
        $('#show_' + this.id).hide();
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文