我无法弄清楚我错在哪里我一直在尝试在SO上找到这么多的可能性和答案,但它并没有给我自定义属性的价值" attr-pid&# 34;
<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));
这适用于我的另一个div,我只有一个类,但是这里有多个,console.log只显示我:
"providersToVisit": []
答案 0 :(得分: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'));
});
答案 1 :(得分: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
函数内修改了数据,除非有其他恶作剧,我们无法看到您从示例中排除了哪些内容。
答案 2 :(得分:0)
是的它可以正常工作,所以我发现了我的错误而且我没有发布整个代码,我真的相信问题出在我发布的代码上。所以我刚刚删除了&#34;如果url ===&#34;
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));
}
}