我如何改变全局变量的值

How can I change value of Global variable

本文关键字:全局变量 改变 何改变      更新时间:2023-09-26

默认变量集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 &B是空的。为什么? ?

我认为问题是你已经分配了"html"变量值为"

+ a + b + "当a &B是空的,然后从来没有改变过。试一试:
     a = 'webb';
     b = 'sam';
     // And append the variable "html"
     html =' <td>'+a+b+'</td>'
     $(div).append(html); 

只是给一些清晰度:这与变量是全局的或局部的无关。你得到空的" <td></td> ",因为变量"html"被分配了一个(即它得到的正常字符串值,而不是对a和b变量的引用)。