我可以在jquery代码中使用原生javascript进行循环吗?
我可以在jquery代码中使用原生javascript进行循环吗?我有一个结构,其中我可以在jquery代码中使用原生javascript进行循环吗?,javascript,jquery,json,Javascript,Jquery,Json,我可以在jquery代码中使用原生javascript进行循环吗?我有一个结构,其中$。每个似乎都不可行。在这种情况下如何循环 for循环中的代码不起作用。如有任何建议,将不胜感激 if (item.relate){ $(prodHeading).append(item.name); if (item.image) {$(prodContent).append(item.image);} if (item.description) { $(prodCont
$。每个似乎都不可行。在这种情况下如何循环
for循环中的代码不起作用。如有任何建议,将不胜感激
if (item.relate){
$(prodHeading).append(item.name);
if (item.image) {$(prodContent).append(item.image);}
if (item.description) {
$(prodContentText).append(item.description);
$(prodContent).append(prodContentText);
}
$(prodBox).append(prodHeading);
$(prodBox).append(prodContent);
//going to look for other attached items
for (j=0; j<10; j++){
var prodBox = $(document.createElement('div')).attr('class','productBox');
var prodHeading = $(document.createElement('div')).attr('class','productBoxHeading');
var prodContent = $(document.createElement('div')).attr('class','productBoxContent');
var prodContentText = $(document.createElement('div')).attr('class','text');
if ('item.image'+j) {$(prodContent).append(item.image);}
if ('item.description'+j) {
$(prodContentText).append(item.description);
$(prodContent).append(prodContentText);
}
$(prodBox).append(prodHeading);
$(prodBox).append(prodContent);
}
}
if(item.relate){
$(prodHeading).append(item.name);
if(item.image){$(prodContent.append(item.image);}
如果(项目说明){
$(prodContentText).append(item.description);
$(prodContent).append(prodContentText);
}
$(prodBox).append(prodHeading);
$(prodBox).append(prodContent);
//要查找其他附加项目吗
对于(j=0;jYes),可以在JQuery中使用常规javascript
这个if语句的作用是什么
if ('item.description'+j)
没有比较发生,所以总是正确的。对于(var j=0;j首先
for (var j=0; j<10; j++){
var prodBox = $(document.createElement('div')).attr('class','productBox');
var prodHeading = $("<div/>",{class:'productBoxHeading'});
var prodContent = $("<div/>",{class:'productBoxContent'});
var prodContentText = $("<div/>",{class:'text'});
if (item.image+j) {prodContent.append(item.image);}
if (item.description+j) {
prodContentText.append(item.description);
prodContent.append(prodContentText);
}
prodBox.append(prodHeading);
prodBox.append(prodContent);
}
没有多大意义。每一个这样的字符串都将传递一个if
条件。如果要查看元素是否存在,请使用:
if ($('item.image'+j).length) {
你想循环什么?你不认为你的j=0中缺少了一个var吗?比如(var j=0;你有什么错误吗?循环本身是标准的,('item.image'+j)实际上是在调用一个json元素。它不是那样工作的。这段代码在$.each循环中。如果我想在所有项之间切换,你能建议怎么做吗?image(0-9)?@amit:你目前正在组合字符串,仅此而已。如果你想访问对象的动态属性,你可以使用item['image'+j]
,等于项。如果j
等于0
,则图像0
,以此类推。
for (var j=0; j<10; j++){
if ('item.image'+j) {
if ($('item.image'+j).length) {