简体   繁体   中英

How do I use raw javascript in jquery and add unique functions to jquery selectors?

I am looking to do what the title says. As I am new to client side programming with java script all to together I do not know the "right" and "proper" way of achieveing what I need done.

I wish to use a simple javascript function

var x;
var items = {};

for (x = 0, x < 7; x++) {
    items[x] = new num;
}

$("li").addclass("items" + num);

Is this right? Am I on the right track even?

I don't know what is num in your code but I suspect you want to get something like this:

$('li').each(function (i) {
    $(this).addClass('items' + i);
});

This will add a class with incrementing index to every li element. If you run $("li").addClass("items" + num) this will add the same class to all li elements.

BTW. JavaScript is case sensitive so you must write addClass instead of addclass .

More tampering....

and I found that if i did

for(var x = 0; x < 7; x++){
    $(".nav li").addClass("items_" + x);
}

I'm still not sure why the above didn't work...

oh well...

RaYell is correct. You need to use $.each or you could use .eq(i) .. ie

for(var x = 0; x < $(".nav li).length(); x++){
    $(".nav li").eq(x).addClass("items_" + x);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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