1.7 js基础,字符串、数组小结
2024-08-30 15:04:47
一、arguments 实参参数的数组
实参【实际的值】,形参【形式上的参数】
当参数个数不固定的时候使用。
示例:
script>
var getSub=function(){
var re=0;
for(var i=0;i<arguments.length;i++){
re+=arguments[i];
}
return re;
}
alert(getSub(1,2,3,4,5,6));//21 </script>
二、批量设置样式
.cssText='width:100px;height:50px';//缺点:会覆盖之前样式
- 有缺陷
with(obj.style){
样式名=样式值;
width=width;(无法设置该属性)
}
3、自定义方法
<script>
function setStyle(obj,json){
for(var name in json){
obj.style[name]=json[name];
}
}
window.onload=function(){
var oDiv=document.getElementById('box');
oDiv.onclick=function(){
setStyle(oDiv,{'width':'200px','height':'300px'});
}
}
</script>
4、字符串常用方法
var str='abd3333';
str.charAt(0); 【获取某一位置的字符】//结果:a
str.indexOf('b');【查找某个字符串的位置,找到返回位置,否则 返回-1】//结果:1,找不到返回-1
str.lastIndexOf('3')【从后往前找】;//结果:6
str.substring(开始位置,结束位置(不包括结束位置));【截取字符串,前包括后不包括】
str.substring(开始位置):【从开始位置截取到最后】
str.toUpperCase();//转换为大写
str.toLowerCase();//转换为小写
str.split('特定的字符');//如果写了一个空字符,每个字符都被分出来。否则按照指定字符进行分割。
str.repalce('',function(){})
str.match(); 匹配出来是数组。
5、字符串比较
字符串数字比较:‘2’>'12' true '156'>'9'.false,比较的是第一个字符,完事后就不往后面比较了。
字符串中文:‘你’>'我',乱来。
字符串大小写字母:‘A’>'a',false
6、数组常用方法
arr.push();//从后面加,返回被添加后的数组的长度。
arr.pop();//从后面干掉,返回被删掉的东西。
arr.shift();//从前面删除,返回被删掉的东西。
arr.unshift();//从前面加
arr.splice(开始的位置,长度);//删除,返回删除的东西,返回了arr
arr.splice(开始的位置,0,要插入的东西)//插入
arr.splice(开始的位置,长度,要替换的东西)//替换(先删除后插入)
arr.join(分隔符);//数组转换成字符串
arr3=arr1.concat(arr2);//数组合并
arr.reverse();//数组反转 welcome to zhinengshe ====>zhinengshe to welcome
arr.sort();//数组排序。
arr.sort(function(num1,num2){
if(num1>num2){
return 1;
}else if(num1<num2){
return -1;
}else{
return 0;
}
})//解决数字排序问题9】
arr.sort(fucntion(num1,num2){return num1-num2});//从小到大简写
arr.sort(fucntion(num1,num2){return num2-num21});//从大到小简写
arr.map(function(val,index,context){return val+1 });//遍历数组,修改数组中每一项,返回新数组,不修改原数组
arr.forEach(function(val,index,context){});//遍历数组,没有返回值,不修改原数组
arr.filter(function(val,index,context){});//筛选数组,返回符合条件的新数组,不修改原数组
最新文章
- Android开发-之监听button点击事件
- Codeforces Round #383 (Div. 2) D. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses(分组背包+dsu)
- Android test---CTS
- spring3 的restful API RequestMapping介绍
- 在MySql 5.0 的表里同时添加两个自动更新的timestamp字段
- 乐视云计算基于OpenStack的IaaS实践
- boost之thread
- lambda形式(转)
- cocos2d-x lua与c++简单交互
- iOS-Reachability的使用
- 简单的GDI+双缓冲的分析与实现
- Condition-线程通信更高效的方式
- AfxMessageBox和MessageBox差别
- python下使用protobuf
- iOS应用开发详解
- react-native 打包apk
- Repository 简化实现多条件查询
- Verilog TestBench Coding Style
- 前端jquery学习--03
- The transaction associated with this command is not the connection&#39;s active transaction