简体   繁体   English

如何使用动态div ID

[英]How to use dynamic div ID

I've been looking at so many answers here that are so close but because my jQuery knowledge is so patchy, I can't make them relevant. 我一直在看这么多答案,这些答案是如此接近,但是由于我的jQuery知识如此零散,所以我无法使它们有意义。 So I'll just go ahead and ask. 所以我就继续问。 I hope you all don't mind! 希望大家不要介意!

So, here's the script: 因此,这是脚本:

$("#button1").mouseenter(function(){
  $("#a1").fadeIn('100', function() { });

  $("#button1").mouseleave(function(){
      $("#a1").fadeOut('100', function() {}); 
  }); 
});

I have multiple buttons which, on mouseenter , I want to activate the corresponding arrow. 我有多个按钮,要在mouseenter上激活相应的箭头。

Rather than repeat the script for each pair - #button2 to #arrow2 etc - how can I just put in some neat variable in the $() bit and have it work? 而不是为每对重复脚本- #button2#arrow2等-我如何才能在$()位中放入一些整洁的变量并使它起作用? Sounds simple, I'm sure there's a way that my denseness cannot find. 听起来很简单,我敢肯定有一种方法无法找到我的密度。

This is the HTML (for those who requested it): 这是HTML(针对那些要求它的人):

 <div id="buttons">
    <p><a href="#"><img src="images/1.png" name="button1" id="button1" /></a></p>
    <p><a href="#"><img src="images/2.png" name="button2" id="button2" /></a></p>
    <p><a href="#"><img src="images/3.png" name="button3" id="button3" /></a></p>
    <p><a href="#"><img src="images/4.png" name="button4" id="button4" /></a></p>
    <p><a href="#"><img src="images/5.png" name="button5" id="button5" /></a></p>
    </div>

<div class="arrow" id="a1"><img src="images/arrow.png" width="747" height="75" /></div>
<div class="arrow" id="a2"><img src="images/arrow.png" width="747" height="75" /></div>
<div class="arrow" id="a3"><img src="images/arrow.png" width="747" height="75" /></div>
<div class="arrow" id="a4"><img src="images/arrow.png" width="747" height="75" /></div>
<div class="arrow" id="a5"><img src="images/arrow.png" width="747" height="75" /></div>
<div class="arrow" id="a6"><img src="images/arrow.png" width="747" height="75" /></div>

Rather than using the ID, give the button a class. 而不是使用ID,而是给按钮一个类。

HTML: HTML:

<button id="button1" class="hoverbutton" />
<button id="button2" class="hoverbutton" />

jQuery: jQuery的:

$(".hoverbutton").mouseenter(function () {
        $(".arrowclass").fadeIn('100', function () { });
    });

$(".hoverbutton").mouseleave(function () {
        $(".arrowclass").fadeOut('100', function () { });
    });
$(".buttonClass").mouseenter(function(){
  $(this).siblings('.aClass').fadeIn('100', function() { });

  $(".buttonClass").mouseleave(function(){
      $(this).siblings('.aClass').fadeOut('100', function() {}); 
  }); 
});

You can use this (or similar, instead of siblings use a proper selector) and add the mentioned classes (aClass and buttonClass) to your components). 您可以使用它(或类似的方法,而不是兄弟姐妹使用适当的选择器),并将提到的类(aClass和buttonClass)添加到组件中。

An example where this works: 一个可行的例子:

<div>
 <button class="buttonClass">Description</button>
 <a class="aClass">Description</a>
</div>

Note that it's important to "group" them inside a div tag, because otherwiese ALL <a> tags will fade in/out when you hover a button. 请注意,将它们“分组”在div标签中非常重要,因为当鼠标悬停在按钮上时,其他所有<a>标签都会淡入/淡出。

Your solution doesn't scale. 您的解决方案无法扩展。

You are assigning eventhandlers for each element in the dom. 您正在为dom中的每个元素分配事件处理程序。

This is one of the beefs I have with jQuery. 这是我使用jQuery的强项之一。 It makes it very easy to do these things wrong and shoot yourself in the foot along the way. 这很容易使这些事情出错,并一路向自己开枪。

What you need is event delegation. 您需要的是事件委托。

Essentially the concept is that you have an event handler much higher up in the dom tree that listens to events that bubble up. 本质上,这个概念是,您在dom树的上方有一个事件处理程序,该事件处理程序侦听冒泡的事件。 So you might be handling your mouse events on a list, not on the list items themselves, but on the document, in a single event handler. 因此,您可能在单个事件处理程序中处理列表中的鼠标事件,而不是列表项本身,而是文档。

Take a look at http://api.jquery.com/on/ 看看http://api.jquery.com/on/

Your code should be something like this: 您的代码应如下所示:

$('body').on('mouseenter', '#buttons img', function (e) {
    $('#a' + $(this).attr('id').slice(-1)).fadeIn(300);
});

$('body').on('mouseleave', '#buttons img', function (e) {
    $('#a' + $(this).attr('id').slice(-1)).fadeOut(300);
});

Notice that I'm actually only using the id to get a link between the buttons and the arrows. 请注意,我实际上仅使用id来获取按钮和箭头之间的链接。 You might consider skipping the id's all together and just go by what index the element has in its parent element. 您可能会考虑一起跳过所有id,然后按元素在其父元素中具有的索引进行遍历。

Working example can be found here: http://jsfiddle.net/GNs44/1/ 可以在以下位置找到工作示例: http : //jsfiddle.net/GNs44/1/

Something like this might get you started: 这样的事情可能会让您入门:

$("[id*=button]").mouseenter(function(){
    var t = $(this),
        idnum = t.slice(-1);

    $("[id=a" + idnum + "]").fadeIn('100', function() {
    }); 
}).mouseleave(function(){
    var t = $(this),
        idnum = t.slice(-1);

    $("[id=a" + idnum + "]").fadeOut('100', function() {
    }); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM