Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从循环中动态创建多个ulli-javascript/jquery/magento_Javascript_Jquery - Fatal编程技术网

如何从循环中动态创建多个ulli-javascript/jquery/magento

如何从循环中动态创建多个ulli-javascript/jquery/magento,javascript,jquery,Javascript,Jquery,我正在尝试读取由magento spConfig创建的全局变量,并在此基础上创建多个无序列表。我的Javascript代码: if(typeof spConfig !='undefined'){ if(typeof spConfig[0] == 'undefined' ) { spConfig[0] = spConfig; } var index = 0 , count = spConfig.length?spConfig.length:1;

我正在尝试读取由magento spConfig创建的全局变量,并在此基础上创建多个无序列表。我的Javascript代码:

   if(typeof spConfig !='undefined'){
    if(typeof spConfig[0] == 'undefined' ) {
        spConfig[0] = spConfig;
    }
    var index  = 0 , count = spConfig.length?spConfig.length:1;
    for( var index  = 0 ; index<count;index++) {
        if(typeof spConfig != 'undefined' && typeof spConfig[index].config != 'undefined' && typeof spConfig[index].config.attributes != 'undefined') {
            for(var attributeID in spConfig[index].config.attributes) {

               //alert(attributeID) gives (the number of ul's i want)
                var ul = jQuery('<ul id="clone'+attributeID+'"></ul>');

                for(var optionID in spConfig[index].config.attributes[attributeID].options) {
                    var option = spConfig[index].config.attributes[attributeID].options[optionID];
                    if(typeof option == 'object') {

                       // alert(option.label); gives the number of li's i want

                        var li = $('<li>'+option.label+'</li>');
                        jQuery(".price-info").append(li);
                        jQuery(".price-info").append(ul);

                    }
                }
            }
        }
    }
}
if(spConfig的类型!=“未定义”){
如果(spConfig[0]的类型=='undefined'){
spConfig[0]=spConfig;
}
var index=0,count=spConfig.length?spConfig.length:1;

对于(var index=0;index您应该首先将
li
s追加到
ul

ul.append(li);
$('.price-info').append(ul);

您应该首先将
li
s附加到
ul

ul.append(li);
$('.price-info').append(ul);

您必须将LIs附加到UL,然后将UL附加到
.price info
UL.append(li);jQuery(“.price info”).append(UL);
,但请注意,这可以优化,在循环中包含字符串或使用文档片段。使用
append()
在这样的循环中,它给出了相同的结果:(那么你确定
jQuery(“.price info”)吗
不返回jq空对象?无论如何,您应该提供复制您的问题的最低限度示例,因为div不是jq空对象。我认为不应该在现在的位置进行追加等。
jQuery(.price info”)。追加(ul)
应该在外部for循环中,在内部for循环之后。但是
ul.append(li);
位于正确的位置。您必须将LIs附加到ul,然后将ul附加到
.price info
ul.append(li);jQuery(.price info”).append(ul);
但要注意,这是可以优化的,在循环中包含字符串或使用文档片段。在这样的循环中使用
append()
确实很慢,它会给出相同的结果:(那么你确定
jQuery(.price-info)吗
不返回jq空对象?无论如何,您应该提供复制您的问题的最低限度示例,因为div不是jq空对象。我认为不应该在现在的位置进行追加等。
jQuery(.price info”)。追加(ul)
应该在外部for循环中,在内部for循环之后。但是
ul.append(li);
在正确的位置,它给出了相同的结果:(我想我没有在正确的地方追加……可能是我应该在下一次追加吗loop@wasiim_dev在开始检查之前,将代码放在JSFIDLE中,以复制您正试图做的事情(即
spConfig
),这可能是有益的。将它放在您的FIDLE中,而不是使用
new Product.Config
spConfig c包含构建ul li所需的所有数据。我只是从我的视图/源代码中复制了代码。这就是我所拥有的全部。它给了我相同的结果:(我想我没有在正确的地方追加……可能是我应该在下一次追加吗loop@wasiim_dev在开始检查之前,将代码放在JSFIDLE中,以复制您正试图做的事情(即
spConfig
),这可能是有益的。将它放在您的FIDLE中,而不是使用
new Product.Config
spConfig c包含构建ul li所需的所有数据。我只是从我的视图/源代码中复制了代码。这就是我真正拥有的全部