2016-11-18 69 views
0

我不知道我在哪里错了我一直在尝试这么多的可能性和答案发现,所以它只是不给我的自定义属性的值“attr-pid “javascript json jquery不能获得自定义属性值

<div class="prod-col et_pb_column et_pb_column_4_4" style=""> 

<div attr-pid="2" class="bbb_prov product_select et_pb_column et_pb_column_2_4 model_container"> 
<div class="icon" attr-pid="2"><img src=" " style=" max-width: 100px;"</div></div> 

<div attr-pid="4" class="bbb_prov product_select et_pb_column et_pb_column_2_4 model_container"> 
<div class="icon" attr-pid="4"><img src=" " style=" max-width: 100px;"></div></div> 

</div> 

我有这个

aProvider = $(".bbb_prov"); 

if (me.savedJSON.providersToVisit == undefined) 
{ 
    me.savedJSON.providersToVisit = []; 
} 

for(var y = 0 ; y < aProvider.length ; y++) 
{ 
    me.savedJSON.providersToVisit.push("&bxb_provider_id="+$(aProvider[y]).attr('attr-pid')); 
} 

CEF_SaveDataJSON(me.savedJSON); 

console.log(JSON.stringify(me.savedJSON, null, 2)); 

这个工作对我的其他分区在那里我只有一个类,但在这儿,它的倍数,只是执行console.log显示我:

"providersToVisit": [] 
+0

'aProvider'是一个jQuery集合。我不确定你可以使用数组符号来访问它们。 – bassxzero

+0

@Santi,当您在jQuery对象上使用'[]'访问器时,它将成为底层对象,所以在这种情况下它将是实际的DOM元素。虽然他们可以使用['.eq(y)'](http://api.jquery.com/eq/)来获取该位置的jQuery包装元素,或者使用['.each()']( http://api.jquery.com/each/)循环访问该集合。 –

+0

是的,你是正确的帕特里克,我只是编辑了我的评论。我进入JSFiddle调查我的猜疑,发现了同样的事情!谢谢。 – Santi

回答

0

所提供的代码工作(见小提琴:https://jsfiddle.net/ta3sgh8e/

HTML

<div class="prod-col et_pb_column et_pb_column_4_4" style=""> 
<div attr-pid="2" class="bbb_prov product_select et_pb_column et_pb_column_2_4 model_container"> 
<div class="icon" attr-pid="2"><img src=" " style=" max-width: 100px;"></div></div> 
<div attr-pid="4" class="bbb_prov product_select et_pb_column et_pb_column_2_4 model_container"> 
<div class="icon" attr-pid="4"><img src=" " style=" max-width: 100px;"></div></div> 
</div> 

JS

function CEF_SaveDataJSON(data){ 
    //What happens in this function? 
    //data.providersToVisit = []; 
} 
me = {savedJSON:{}}; 
aProvider = $(".bbb_prov"); 
if (me.savedJSON.providersToVisit == undefined) 
{ 
    me.savedJSON.providersToVisit = []; 
} 
for(var y = 0 ; y < aProvider.length ; y++) 
{ 
    me.savedJSON.providersToVisit.push("&bxb_provider_id="+$(aProvider[y]).attr('attr-pid')); 
} 
CEF_SaveDataJSON(me.savedJSON); 
console.log(JSON.stringify(me.savedJSON, null, 2)); 

我怀疑的是,数据在CEF_SaveDataJSON函数内部控制台日志之前修改,除非有其他的恶作剧我们无法看到您已经排除你的榜样。

0

小提琴http://jsbin.com/kutudogopo/edit?html,js,console

试试这个。既然你只是循环遍历它们,你可以使用JQuery的.each方法遍历它们。

HTML:

<div class="prod-col et_pb_column et_pb_column_4_4" style=""> 

<div attr-pid="2" class="bbb_prov product_select et_pb_column et_pb_column_2_4 model_container"> 
<div class="icon" attr-pid="2"><img src=" " style=" max-width: 100px;"</div></div> 

<div attr-pid="4" class="bbb_prov product_select et_pb_column et_pb_column_2_4 model_container"> 
<div class="icon" attr-pid="4"><img src=" " style=" max-width: 100px;"></div></div> 

JQuery的:

var aProvider = $(".bbb_prov"); 
console.log(aProvider.length); 

$.each(aProvider, function(){ 
    var $this = $(this); 
    console.log($this.attr('attr-pid')); 

}); 
0

是的,它的工作原理是这样的,所以我发现我的错误和我的坏不发布整个代码我真的相信问题是在我张贴的代码。所以,我只是去掉了 “如果URL ===”

this.onPageLoad = function() 
{ 
    var url = window.location.href; 
    var aModels; 
    var aProvider; 

    //iphone 
    if (url == "http://blablabla.com/apple/?menuf_id=1") //This is were i screwed up and had to remove it. 
    { 
     aModels = $(".bbb_prov"); 


     if (me.savedJSON.modelsToVisit == undefined) { 
      me.savedJSON.modelsToVisit = []; 
     } 

     if (me.savedJSON.phoneModel == undefined) { 
      me.savedJSON.phoneModel = []; 
     } 

     for(var i = 0 ; i < aModels.length ; i++) 
     { 
      me.savedJSON.modelsToVisit.push("http://blablabla.com/apple/?menuf_id=1&bxb_model_id="+$(aModels[i]).attr('attr-mid')); 
     } 

     for(var x = 0 ; x < aModels.length ; x++) 
     { 
      me.savedJSON.phoneModel.push($(aModels[x]).text()); 
     } 

     CEF_SaveDataJSON(me.savedJSON); 


     me.blablabla_GotoNextModel(); 

     aProvider = $(".bbb_prov"); 

     if (me.savedJSON.providersToVisit == undefined) { 
      me.savedJSON.providersToVisit = []; 
     } 


     for(var y = 0 ; y < aProvider.length ; y++) 
     { 
      me.savedJSON.providersToVisit.push("&bxb_provider_id="+$(aProvider[y]).attr('attr-pid')); 
     } 

     CEF_SaveDataJSON(me.savedJSON); 

     console.log(JSON.stringify(me.savedJSON, null, 3)); 
    } 

}