让javascrip函数内的局部变量能为全局使用的方法

admin5年前前端技术1736
<script type="text/javascript">
  var mine;
 function me() {
   mine=$("#my").html();
    console.log(mine)
 }
 me();
  console.log(mine);
 

</script>

特别注意:

1.jquery 函数内或回调函数内的变量只有同样在另一个回调函数内能公用一个变量;

2.外部用var定义这个变量,那么内部就不能var这个保留字定义这个变量

3.或者变量不用var去定义,代码如下:

<script type="text/javascript">
 
 function me() {
   mine=1;
    console.log(mine)
 }
 me();
  console.log(mine);
 

</script>


相关文章

分享与定时自动跳转

<!DOCTYPE html> <html> <head> <meta charset="utf-8">...

关于css的position参数自我领悟心得整理

relative :相对与最近的DOM定位   absolute :相对里面的绝对定位,跟relative 一起用,起到随心所欲定位的效果sticky :滚顶地位定位,用与鼠标滚动...

让TD 与div自动换行的css方法

word-wrap: break-word;  word-break: break-all; white-space: normal;...

webpack全局安装了,使用命令时却提示安装脚手架

全局安装了webpack ,使用webpack的时候,却有如下的提示One CLI for webpack must be installe...

Navigate down the menu through jquery development

The core knowledge is to use jquery's hover method to achieve, The animation effe...

H5页面移动端双击屏幕禁止页面放大

在meta标签中加入user-scalable=no属性...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。