如何使用jquery child()方法在许多元素中添加类?

时间:2015-05-12 08:38:25

标签: jquery

我编写了一个用红色边框标记和控制div元素的函数。它由select选项元素控制。选项是kotak1,kotak2等。因此,当我选择kotak1时,相关的div边界变为红色。当我更改其他选项时,如何将div元素再次恢复为黑色边框?

HTML

<div id="group-select">
    <select id="select_kotak">
        <option selected>Pilih Kotak</option>
    </select>
</div>
<br>
<div id="group-kotak">
    <div class="kotak-nonaktif">Kotak 1</div>
    <div class="kotak-nonaktif">Kotak 2</div>
    <div class="kotak-nonaktif">Kotak 3</div>
    <div class="kotak-nonaktif">Kotak 4</div>
</div>
.kotak-nonaktif {
    height: 100px;
    width: 100px;
    border: 1px solid black;
    float: left;
    margin-left: 1px;
}
.kotak-aktif {
    height: 100px;
    width: 100px;
    border: 3px solid red;
    float: left;
    margin-left: 1px;
}
$(document).ready(function () {
    set_pasar_option();
});

function set_pasar_option() {
    for (i = 1; i < 5; i++) {
        var option = document.createElement("option");
        option.text = "Kotak" + i;
        option.value = "value" + i;
        option.id = "id" + i;
        var select = document.getElementById("select_kotak");
        select.appendChild(option);
        option.addEventListener('click', function () {
            aktif_kotak(this.id);
        });
    }
}

function non_aktif_kotak() {
    var allkotak = document.getElementById('groupkotak').children();
    $(allkotak).addClass("kotak-nonaktif");
}

function aktif_kotak(id) {
    //non_aktif_kotak(); 
    id = id.substr(2, 4);
    id = parseInt(id) - 1;
    var kotak_aktif = document.getElementById('group-kotak').children[id];
    $(kotak_aktif).addClass("kotak-aktif");
}

FIDDLE

1 个答案:

答案 0 :(得分:1)

只需在FBSDKLoginManager* loginManager = [[FBSDKLoginManager alloc] init]; [loginManager logInWithReadPermissions:@[@"user_posts"] handler:^(FBSDKLoginManagerLoginResult *result, NSError* error) { if (error) { // Process error NSLog(@""); } else if (result.isCancelled) { // Handle cancellations NSLog(@""); } else { // If you ask for multiple permissions at once, you // should check if specific permissions missing if ([result.grantedPermissions containsObject:@"user_posts"]) { FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/10002434324342/posts" parameters:nil HTTPMethod:@"GET"]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { // Handle the result if (error) { NSLog(@""); } }]; } } }]; 功能中添加此行:

aktif_kotak()

在最后一次这样。

$(kotak_aktif).siblings().removeClass("kotak-aktif");

<强> DEMO