开发者

function that has parameters in javascript/jquery won't work

I have values in my function but it says it isn't defined.

This is my code:

<img onload="getFullnameDetails(263,225)" src="'+getBaseURL()+'js/check/no-image.png" rel="fullname" />

function getFullnameDetails(mainHeight,upperstyle){
setTimeout("fullnameCenter(mainHeight,upperstyle)",2000);
}

function fullnameCenter(mainHeight,upperstyle){
    var distOfMainAndUpper = 38;
    var mainHalfHeight = 131.5;
    var imageHeight = jQuery("img[rel='fullname']").height(); //there is a delay
    var imageHalfHeight开发者_高级运维 = imageHeight/2;
    var fromImageTopToMainHalf = mainHalfHeight - imageHeight;
    var position = imageHalfHeight+fromImageTopToMainHalf-distOfMainAndUpper;
    jQuery(".test1").css("bottom",position+"px");
}

It says here that mainHeight is not defined. Why is this happening. This happens on this line: setTimeout("fullnameCenter(mainHeight,upperstyle)",2000);

Thanks in advance ;)


Try this

function getFullnameDetails(mainHeight,upperstyle){
    setTimeout(function() {fullnameCenter(mainHeight,upperstyle);},2000);
}


When you set a timeout, it runs in the global scope so mainHeight and upperstyle are no longer in scope. You are better off using an anonymous function and providing the parameters:

setTimeout(function() {
    fullnameCenter(mainHeight,upperstyle);
}, 2000);


  var imageHeight = jQuery("img[rel='fullname']").height(); //there is a delay

you get the jquery image object and it not work! and let me tel you a secret you don't need the delay try this:

var imageHeight = jQuery("img[rel='fullname']")[0].height;

you see that i added the "[0]" after the jquery selector it will return the html image element and not the image jquery object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜