如何更改全局变量的值

时间:2013-06-09 15:36:42

标签: javascript jquery variables global

默认变量集var a = ''; var b = ''; var html =' <td>'+a+b+'</td>';

然后我使用ajax创建另一个事件

$('element').click(function(){
....

         //when ajax was success  change value of variable 
         a = 'webb';
         b = 'sam';
         // And append the variable "html"
         $(div).append(html );  

但变量a&amp; b是空的。为什么??

1 个答案:

答案 0 :(得分:1)

我认为问题是你已经为“&lt; td&gt; + a + b +&lt; / td&gt;”指定了“html”变量值什么时候&amp; b是空的,然后从未改变它。 尝试:

     a = 'webb';
     b = 'sam';
     // And append the variable "html"
     html =' <td>'+a+b+'</td>'
     $(div).append(html); 

只是为了清楚一点:这与全局或本地变量无关。你得空“<td></td>”,因为变量“html”被分配了一个(即它获得了正常的字符串值,而不是对a和b变量的引用)。