我需要一个肝脏,我试图将动态网址传递给一个函数。
码
onload="getImage('+ val.logo +');" onclick="loadPageMerchantInfo('+ val.merchant_id +');
val.logo拥有这样的网址:{{3}}
我需要将此网址传递给
以下的功能function getImage(url)
{
setStorage("merchant_logo",url);
var logo = getStorage("merchant_logo");
onsenAlert(logo);
return logo;
}
function loadPageMerchantInfo(str)
{
var merlogos = getImage();
onsenAlert(merlogos);
setStorage("merchant_id",str);
var options = {
animation: 'none',
onTransitionEnd: function() {
displayMerchantLogo2(
merlogos,
'' ,
'page-merchantinfo'
);
callAjax("getMerchantInfo","merchant_id="+ getStorage('merchant_id'));
}
};
sNavigator.pushPage("merchantInfo.html", options);
merhantPopOverMenu.hide();
}
因此,当我打印该值时,没有任何内容,只显示“未定义”。
答案 0 :(得分:0)
在html中:
onload="getImage(val.logo);";
js内部:
window.onload=()=>getImage(val.logo);
window.onclick=()=>loadPageMerchantInfo(val.merchant_id);
您可以查看箭头功能(正常情况也可以)和闭包。
为什么你的代码不起作用:
window.onload="a string";
//the js engine does:
window.onload();
//wich will cause an error:
ERROR: window.onload is not a function!
如果你真的想要这个糟糕的样式字符串,你需要将String转换为一个函数:
onload=new Function("getImage("+ val.logo +");");
onclick=new Function("loadPageMerchantInfo("+ val.merchant_id +");");
但是,每个JSlover都会因此而杀了你。