简体   繁体   English

使用jQuery定位多个div

[英]Target multiple divs with jQuery

I can't tell you how much I would appreciate some help with this. 我无法告诉您,对此我能提供多少帮助。

I am trying to have three buttons, so that visitors can press a button and display pricing in their preferred currency. 我尝试使用三个按钮,以便访问者可以按一个按钮并以其首选货币显示价格。

I understand how to do this with one column beneath But as you will see, when I am applying it to multiple columns, only the first one works (with the price switching when the button is clicked). 我知道如何在下面的一列中执行此操作,但是正如您将看到的,当我将其应用于多列时,只有第一个有效(单击按钮时会切换价格)。

I understand that id's in html cannot be repeated, and that this is likely the source of the problem. 我知道html中的ID无法重复,这很可能是问题的根源。 However I do not know what to do to make the targeting work on something other than ID's. 但是,我不知道该怎么做才能使定位在ID以外的其他东西上起作用。

I have pasted all of the code into this fiddle: http://jsfiddle.net/Rd7mV/ - note that the prices which should be changed are highlighted in red on the fiddle. 我已将所有代码粘贴到该小提琴中: http : //jsfiddle.net/Rd7mV/-请注意,应该更改的价格在小提琴上以红色突出显示。

The javascript is here: JavaScript在这里:

jQuery("#menu a").click(function () {
    var link = jQuery(this).attr('href');
    var showIt = jQuery(link);
    var hideIt = jQuery(".cc.current");

    hideIt.fadeOut(100, function () {
        hideIt.removeClass("current");
        showIt.addClass("current");
        showIt.fadeIn(100);
    });
});

Please, please if someone can help, this would be a lifesaver. 拜托,如果有人可以帮助,那将是一个救命稻草。 I have searched high and low, and just really need some help on the implementation. 我已经搜寻了很多东西,但实际上确实需要一些帮助。

Thank you. 谢谢。 AB. AB。

Instead of trying to show all the IDs of #currencypounds, for example, try using classes. 例如,不要尝试显示#currencypounds的所有ID,请尝试使用类。

You have hidden DIVs for each section with the IDs #currencypounds, #currencyeuros, #currencydollars. 对于每个部分,您具有ID#currencypounds,#currencyeuros,#currencydollars的隐藏DIV。 Change those to classes instead of IDs, then use your links to show a specific class and hide the rest of the classes. 将其更改为类而不是ID,然后使用链接显示特定的类并隐藏其余的类。 I don't have time to change this all for you, but you should be able to figure it out. 我没有时间为您改变这一切,但是您应该能够弄清楚。

Use classes instead of id, example, 例如,使用类代替ID

You give a class to multiple elements that have class dollar. 您将一个类分配给具有class美元的多个元素。 You may select those elements now by, 您现在可以选择这些元素,

$(".dollar") . $(".dollar") Now for example you need to removeClass current from all the elements with class dollar, use, 现在,例如,您需要从所有美元类,使用,

$(".dollar").removeClass('current');

This is a working version: jsFiddle . 这是一个工作版本: jsFiddle

Did the following: 做了以下事情:

  1. Instead of using the href to store the currency used a custom attribute data-targetCurrency (this is better to prevent the behaviour of href): 与其使用href来存储货币data-targetCurrency使用自定义属性data-targetCurrency (这样可以更好地防止href的行为):

     <a href="javascript: void(0);" data-targetCurrency="currencyeuros"><button class="btn btn-danger"> 
  2. Instead of using id , used another custom attribute data-currency : 除了使用id ,还使用了另一个自定义属性data-currency

     <div class="cc" data-currency="currencyeuros">&euro;159</div> 
  3. Changed the javascript to: 将javascript更改为:

     jQuery("#menu a").click(function () { var currency = jQuery(this).attr('data-targetCurrency'); var showIt = jQuery("[data-currency=" + currency + "]"); var hideIt = jQuery(".cc.current"); hideIt.fadeOut(100, function () { hideIt.removeClass("current"); showIt.addClass("current"); showIt.fadeIn(100); }); }); 

NB: 注意:

The jQuery selector for an attribute is jQuery("[attributename = attributevalue]") 属性的jQuery选择器是jQuery("[attributename = attributevalue]")

I would look into data attributes. 我会研究数据属性。 Obviously your jQuery is simply targeting the first item, not all. 显然,您的jQuery只是针对第一项,而不是全部。 Take a look at the fiddle which targets multiple divs. 看一下针对多个div的小提琴。

jQuery Show and Hide multiple divs with a selected class jQuery使用选定的类显示和隐藏多个div

http://jsfiddle.net/fKHsB/ http://jsfiddle.net/fKHsB/

jQuery(function() {
jQuery('.buttons a').click(function() {
    $(this).addClass('selected').siblings().removeClass('selected');
    var target = $(this).data('target');
    if (target === 'all') {
        jQuery('.targetDiv').show();
    } else {
        jQuery('.targetDiv').hide();
        jQuery('#div' + target).show();
    }
});

}); });

Yes, your problem is that you have multiple elements with the same id. 是的,您的问题是您有多个具有相同ID的元素。 You need to reference classes instead of id's. 您需要引用类而不是ID。

I've modified your fiddle to make it work. 我已经修改了您的小提琴以使其正常运行。 I changed the id's to classes. 我将ID更改为类。

http://jsfiddle.net/kMYye/1/ http://jsfiddle.net/kMYye/1/

<div class="cc current currencypounds">&pound;1369</div>

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

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