开发者

Cannot get a jquery variable to pass it's declared value

--In the variable line below I declared a variable (xwid) as 690. But when I try the code by using the variable of $xwid, the width never updates. Any ideas?

$xwid=690;  // my var

// my code
$('.iframe-link').html( '<iframe src="reasons.html" frameborder="0" 开发者_开发知识库width=$xwid height="305" scrolling="auto">


Variables in Javascript are not declared this way

$xwid=690;     // wrong
alert($xwid);  // wrong

var xwid = 690;  // right
xwid = 690;      // right
alert(xwid);     // right

EDIT: Your code should look like thiis

var xwid=690;  // my var

// my code
$('.iframe-link').html('<iframe src="reasons.html" frameborder="0" width="'+xwid+'" height="305" scrolling="auto">');


I would say

var xwid=690;  // my var

// my code
$(".iframe-link").html( '<iframe src="reasons.html" frameborder="0" width='+xwid+' height="305" scrolling="auto">');


Unless you mean to declare your variables as global, you need to put a 'var' in front. Hence you would declare the variable in your code as:

var $wxid = 690;

Also, you don't need to put the dollar symbol in front of the variable name. It isn't bad (in that it will work fine) but it isn't required and looks a bit strange.

Now to answer the question. The problem you are having is that unlike PHP, Javascript doesn't expand variables in strings.You need to do concatenate the strings using the + operator. Something like this

$('.iframe-link').html('<iframe src="reasons.html" frameborder="0" width="'+$xwid+'" height="305" scrolling="auto">');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜