简体   繁体   English

如何在jquery中使用原始javascript并将唯一函数添加到jquery选择器?

[英]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. 因为我是使用Java脚本进行客户端编程的新手,所以我不知道实现我需要完成的“正确”和“正确”的方法。

I wish to use a simple javascript function 我希望使用一个简单的javascript函数

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: 我不知道您的代码中的num是什么,但我怀疑您想得到这样的东西:

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

This will add a class with incrementing index to every li element. 这将为每个li元素添加一个索引递增的类。 If you run $("li").addClass("items" + num) this will add the same class to all li elements. 如果运行$("li").addClass("items" + num)这将向所有li元素添加相同的类。

BTW. BTW。 JavaScript is case sensitive so you must write addClass instead of addclass . JavaScript区分大小写,因此您必须编写addClass而不是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 您需要使用$.each ,也可以使用.eq(i) ..即

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

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

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