简体   繁体   English

Javascript:一次调用中有多个GetElementByID

[英]Javascript: Multiple GetElementByID's in one call

Really simple question. 真的很简单的问题。 I have the following code, and would like to consolidate it down as much as possible. 我有以下代码,并希望尽可能地将其合并。 Can't get it to work though.. tried using commas etc. Is there a way to call more than one ID in the GetElementByID method? 无法让它工作但尝试使用逗号等。有没有办法在GetElementByID方法中调用多个ID? New to Javascript : / Javascript新手:/

$("#set50").live("click", function() {
    document.getElementById("statusBox50").setAttribute("class", "statusBoxSelected");
    document.getElementById("statusBox25").setAttribute("class", "statusBox");
    document.getElementById("statusBox75").setAttribute("class", "statusBox");
    document.getElementById("statusBox100").setAttribute("class", "statusBox");
    $(".statusbar").animate({
        width: '115px'
    }, 500);    
});

No, but since you're using jQuery anyway, you can use that. 不,但是既然你正在使用jQuery,你可以使用它。 This will add the statusBox class to the elements with the IDs statusBox25 , statusBox75 , and statusBox100 : 这会将statusBox类添加到ID为statusBox25statusBox75statusBox100

$("#statusBox25, #statusBox75, #statusBox100").addClass('statusBox');

Alternatively, if you want to remove all of the existing classes and replace them all with statusBox like your original code as doing, you could use this: 或者,如果要删除所有现有类并使用statusBox替换它们,就像执行原始代码一样,可以使用:

$("#statusBox25, #statusBox75, #statusBox100").attr('class', 'statusBox');

Is there a way to call more than one ID in the GetElementByID method? 有没有办法在GetElementByID方法中调用多个ID?

No, and it is Id (with a lower case d) 不,它是Id(小写d)

And don't use setAttribute , it is broken in all but the most recent versions of IE. 并且不要使用setAttribute ,除了IE的最新版本之外,它都被破坏了。

You're using jQuery, so you can use its helper functions: 你正在使用jQuery,所以你可以使用它的辅助函数:

jQuery('#statusBox50').addClass('statusBoxSelected');
jQuery('#statusBox25, #statusBox75, #statusBox100').addClass('statusBox');

You should be able to do something like: 你应该可以这样做:

$("#set50").live("click", function() {
    $("#statusBox50").addClass("selected");
    $("#statusBox25, #statusBox75, #statusBox100").removeClass("selected");
    $(".statusbar").animate({...});
});

Note that I am doing something different to you: I have a class "selected" that I either apply or remove, rather than setting the classes of [statusBoxSelected|statusBox]. 请注意,我正在做一些与您不同的事情:我有一个“已选择”的类,我应用或删除,而不是设置 [statusBoxSelected | statusBox]的类。 This could be in addition to a class="statusBox". 这可能是class =“statusBox”的补充。

I'd also recommend you look at other ways that don't hard-code the ids like that. 我还建议您查看其他不会像这样硬编码ID的方法。 For instance: 例如:

$(".set50").live("click", function(evt) {
    $('.statusBar').removeClass("selected");
    $('#statusBar50').addClass("selected");
});

You could even go further, and have an attribute on the "set" elements that contained the amount the status bar should be set to. 你甚至可以走得更远,并在“set”元素上有一个属性,其中包含状态栏应该设置的数量。

Or consider using an HTML5 progress bar, and some shims to do the same in non-supported browsers. 或者考虑使用HTML5进度条,以及一些垫片在不支持的浏览器中执行相同操作。

$("#set50").live("click", function() {
    $("statusBox50").addClass("statusBoxSelected");
    $("statusBox25").addClass"statusBox");
    $("statusBox75").addClass( "statusBox");
    $("statusBox100").addClass( "statusBox");
    $(".statusbar").animate({
        width: '115px'
    }, 500);    
});

is this what u want ? 这是你想要的吗?

.addClass adds a class to the selected element.[ docs ] .addClass为所选元素添加一个类。[ docs ]

EDIT : As u said u want a single. 编辑 :正如你所说,你想要一个单一的。 Just assign a class to all the elements u want to add the class. 只需为要添加类的所有元素分配一个类。 For eg: i assign the class adding . 例如:我指定类adding then u can addclass to all the elements like this : 那么你可以添加类到这样的所有元素:

$('.adding').addClass('statusBox')

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

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