$(function() {})

$(function() {});是$(document).ready(function(){ })的简写,最早接触的时候也说$(document).ready(function(){ })这个函数是用来取代页面中的window.onload;但是今天发现 好像不是这样回事!是在做一个页面载入效果时发现的!
 

$(document).ready() 里的代码是在页面内容都加载完才执行的,如果把代码直接写到script标签里,当页面加载完这个script标签就会执行里边的代码了,此时如果你标签里执行的代码调用了当前还没加载过来的代码或者dom,那么就会报错,当然如果你把script标签放到页面最后面那么就没问题了,此时和ready效果一样。

$(document).ready(function(){})可以简写成$(function(){});

点击段落后,此段落隐藏:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head>
<script src= "http://libs.baidu.com/jquery/1.9.0/jquery.js" type= "text/javascript" ></script>
<script type= "text/javascript" >
$(document).ready( function (){
  $( "p" ).click( function (){
   $( this ).hide();
  });
});
</script>
</head>
 
<body>
  <p>If you click on me, I will disappear.</p>
</body>
</html>

如果把$(document).ready(function() {});去掉后,无法隐藏段落:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html>
<head>
<script type= "text/javascript" src= "jquery.js" ></script>
<script type= "text/javascript" >
  $( "p" ).click( function (){
   $( this ).hide();
  });
</script>
</head>
 
<body>
  <p>If you click on me, I will disappear.</p>
</body>
</html>

但是把script放到页面最后的话,就可恢复隐藏效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
</head>
 
<body>
  <p>If you click on me, I will disappear.</p>
</body>
 
<script type= "text/javascript" src= "jquery-1.7.2.min.js" ></script>
<script type= "text/javascript" >
  $( "p" ).click( function (){
    $( this ).hide();
  });
</script>
</html>

总结:

$(document).ready 里的代码是在页面内容都加载完才执行的,你直接写到script标签里,当页面加载完这个script标签就会执行里边的代码了,如果你标签里执行的代码调用了当前还没加载过来的代码或者dom,那么就会报错,
当然如果你把script标签当到页面最后面那么就没问题了和ready差不多的效果

转载于:https://www.cnblogs.com/superAnny/p/6513391.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
$script:THIS_PATH = $myinvocation.mycommand.path $script:BASE_DIR = Split-Path (Resolve-Path "$THIS_PATH/..") -Parent function global:deactivate([switch] $NonDestructive) { if (Test-Path variable:_OLD_VIRTUAL_PATH) { $env:PATH = $variable:_OLD_VIRTUAL_PATH Remove-Variable "_OLD_VIRTUAL_PATH" -Scope global } if (Test-Path function:_old_virtual_prompt) { $function:prompt = $function:_old_virtual_prompt Remove-Item function:\_old_virtual_prompt } if ($env:VIRTUAL_ENV) { Remove-Item env:VIRTUAL_ENV -ErrorAction SilentlyContinue } if (!$NonDestructive) { # Self destruct! Remove-Item function:deactivate Remove-Item function:pydoc } } function global:pydoc { python -m pydoc $args } # unset irrelevant variables deactivate -nondestructive $VIRTUAL_ENV = $BASE_DIR $env:VIRTUAL_ENV = $VIRTUAL_ENV New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH $env:PATH = "$env:VIRTUAL_ENV/Scripts;" + $env:PATH if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) { function global:_old_virtual_prompt { "" } $function:_old_virtual_prompt = $function:prompt if ("" -ne "") { function global:prompt { # Add the custom prefix to the existing prompt $previous_prompt_value = & $function:_old_virtual_prompt ("() " + $previous_prompt_value) } } else { function global:prompt { # Add a prefix to the current prompt, but don't discard it. $previous_prompt_value = & $function:_old_virtual_prompt $new_prompt_value = "($( Split-Path $env:VIRTUAL_ENV -Leaf )) " ($new_prompt_value + $previous_prompt_value) } } }
最新发布
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值