开发中常用的js命令

1、获得窗口长宽高

   $(window).width();//浏览器当前窗口可视区域宽度

  $(document).width();//浏览器窗口文档的宽度

  $(document.body).width();//浏览器当前窗口body的宽度

2、offset属性

 包含2个属性:top和left

例子:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>offset demo</title>
<style>
p {
margin-left: 10px;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<p>Hello</p><p>2nd Paragraph</p>
 
<script>
var p = $( "p:last" );
var offset = p.offset();
p.html( "left: " + offset.left + ", top: " + offset.top );
</script>
 
</body>
</html>

  

3、scrollTop()和scrollLeft()属性(注意:如果窗口不滚动或者非常高,那就是0)
  
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>scrollLeft demo</title>
  <style>
  p {
    margin: 10px;
    padding: 5px;
    border: 2px solid #666;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<p>Hello</p><p></p>
 
<script>
var p = $( "p:first" );
$( "p:last" ).text( "scrollLeft:" + p.scrollLeft() );
</script>
 
</body>
</html>

4、split()

    var str="a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a";

   var arr=str.split("/");

   console.log(arr);

   console.log(str.length);

5、join()

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr.join("."))

6、substring()

var str="abcdefg";

var ob=str.substring(0,5);

console.log(ob);

7、replace()

var str="1996-4-6";

var ob=str.replace(/-/g,"");

console.log(ob);

8、indexOf()(注意:对大小写敏感)

function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.indexOf(str2);
return(s);
}

还有更多笔记会在后续补充!

 

 

 
   
 
 
posted @ 2017-07-08 14:15  @circle  阅读(1126)  评论(1编辑  收藏  举报