jQuery CSS到多个元素? [英] jQuery css to multiple elements?

查看:90
本文介绍了jQuery CSS到多个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将此脚本应用于其他两个div的相同方式: 效果很好,但是如何在不重写每个元素的情况下使它对其他两个元素执行相同的操作呢?

I need to apply this script to work the same way for two other divs: This works well, but how can I make it do the same thing for two other elements without rewriting the entire thing for each?

$(".survey_option li > a").each().live('click', function (event) {
    // First disable the normal link click
    event.preventDefault();

    // Remove all list and links active class.
    $('.so_1 .active').toggleClass("active");
    // Grab the link clicks ID
    var id = this.id;

    // The id will now be something like "link1"
    // Now we need to replace link with option (this is the ID's of the checkbox)
    var newselect = id.replace('partc', 'optionc');

    // Make newselect the option selected.
    $('#'+newselect).attr('checked', true);

    // Now add active state to the link and list item
    $(this).addClass("active").parent().addClass("active");

    return false;
});

推荐答案

您可以通过更改选择器使它影响多个元素:

You can cause it to affect multiple elements by changing your selector:

$(".element_1, .element_2, .randomElement, #someOtherThing");

还请注意,在添加$.live()调用之前,无需调用$.each(). $.live()将应用于所有匹配的元素,并执行其自己的内部$.each()逻辑.

Note also that you don't need to call $.each() before adding your $.live() call. $.live() will be applied to all of the matched elements, doing its own internal $.each() logic.

$(".elem1, .elem2 > a, #someThing").live("click", function(e){
  e.preventDefault();
  alert($(this).text()); // works on all elements in the selector
});

这篇关于jQuery CSS到多个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆