项目中的那些事---JavaScript
一、String.charAt(index)
作用:获取字符串指定索引位置的字符
注意:index的值是0~(字符串长度-1)之间的值
<script type="text/javascript">
var str ="my name is javascript";
var c2 = str.charAt(2);
var c3 = str.charAt(3);
var c6 = str.charAt(6);
var c11 = str.charAt(11);
var c15 = str.charAt(15);
console.info("第3个位置的字符是"+c2);
console.info("第4个位置的字符是"+c3);
console.info("第7个位置的字符是"+c6);
console.info("第12个位置的字符是"+c11);
console.info("第16个位置的字符是"+c15);
</script> 运行结果是:
第3个位置的字符是
第4个位置的字符是n
第7个位置的字符是e
第12个位置的字符是j
第16个位置的字符是s
二、String.substring(start[,end])和String.substr(start[,length])
1、String.substring(start[,end])):返回指定开始位置和结束位置之间的字符串
参数所代表的意义
start:字符串起始位置,必选项,是一个非负数。
end:字符串终止位置,可选项,是一个非负数
结果
截取的结果是从start~end-1之间的长度为end-start的字符串,即截取的子串中不包含最后end位置的字符
参数的限制
当start=end时,截取的子串是”“
当start>end时,截取之前会自动交换位置
当start或end为负数时,会自动替换为0
当end不填时会截取从start开始到字符串结尾处的子串
2、String.substri(start[,length])):返回从start开始,长度为length的字符串
参数所代表的意义
start:字符串起始位置,必选项,非负数
length:要截取的子串的长度,可选项,非负数
结果
截取的是从start位置开始,长度为length的子串
参数的限制
当start为负数时,start=str.length+start
当length为0时返回的子串是""
当length不选时返回start到字符串结尾处的子串
下面是代码示例:
<script type="text/javascript">
testSubstr();
function testSubstr(){
var str = "0123456789";
console.info(str.substring(0));//
console.info(str.substring(3));//
console.info(str.substring(10));//""
console.info(str.substring(12));//""
console.info(str.substring(-2));//
console.info(str.substring(0,5));//
console.info(str.substring(0,9));//
console.info(str.substring(0,10));//
console.info(str.substring(2,7));//
console.info(str.substring(0,-2));//""
console.info(str.substring(3,-4));//
console.info(str.substring(-2,-6));//"" console.info(str.substr(0));//
console.info(str.substr(3));//
console.info(str.substr(10));//""
console.info(str.substr(12));//""
console.info(str.substr(-2));//
console.info(str.substr(0,5));//
console.info(str.substr(0,9));//
console.info(str.substr(0,10));//
console.info(str.substr(2,7));//
console.info(str.substr(0,-2));//""
console.info(str.substr(3,-4));//""
console.info(str.substr(-2,-6));//""
}
</script>
三、typeof(var)函数
作用:检测给定变量的数据类型
JavaScript中的数据类型是弱类型,一个变量var可以用来存放各种数据类型。
下面通过代码来测试:
function testTypeof(){
console.info(typeof("hello")); //string
console.info(typeof(11)); //number
console.info(typeof(true)); //boolean
console.info(typeof(document)); //object
console.info(typeof(click)); //undefined
console.info(typeof(function head(){})); //function
}
testTypeof();
浏览器控制台的执行结果如下:
string
number
boolean
object
undefined
function
function testTypeof(){
console.info("------包含括号的方式------");
console.info(typeof("hello")); //string
console.info(typeof(11)); //number
console.info(typeof(true)); //boolean
console.info(typeof(document)); //object
console.info(typeof(click)); //undefined
console.info(typeof(function head(){})); //function
console.info("------不包含括号的方式------");
console.info(typeof "world"); //string
console.info(typeof 23); //number
console.info(typeof false); //boolean
console.info(typeof onclick); //object
console.info(typeof click); //undefined
console.info(typeof function foot(){}); //function
}
testTypeof();
浏览器控制台输出结果:
------包含括号的方式------ base1.js:48
string base1.js:49
number base1.js:50
boolean base1.js:51
object base1.js:52
undefined base1.js:53
function base1.js:54
------不包含括号的方式------ base1.js:55
string base1.js:56
number base1.js:57
boolean base1.js:58
object base1.js:59
undefined base1.js:60
function
项目中的那些事---JavaScript的更多相关文章
- 记录下项目中常用到的JavaScript/JQuery代码二(大量实例)
记录下项目中常用到的JavaScript/JQuery代码一(大量实例) 1.input输入框监听变化 <input type="text" style="widt ...
- 1000多个项目中的十大JavaScript错误以及如何避免
通过统计数据库中的1000多个项目,我们发现在 JavaScript 中最常出现的错误有10个.下面会向大家介绍这些错误发生的原因以及如何防止. 对于这些错误发生的次数,我们是通过收集的数据统计得出的 ...
- 项目中的那些事---Java反射的应用
最近工作中遇到一个这样的问题: 为某个项目中的所有接口做一个测试工具,使用java Swing技术,该项目有不同的版本,不是所有版本中的接口都是相同的,而我做的工具需要兼容所有版本. 于是就引入了这样 ...
- 记录下项目中常用到的JavaScript/JQuery代码一(大量实例)
一直没有系统学习Javascript和Jquery,每次都是用到的时候去搜索引擎查,感觉效率挺低的.这边把我项目中用的的记录下,想到哪写哪,有时间再仔细整理. 当然,由于我主要是写后端java开发,而 ...
- 项目中的那些事---下载pdf文件
最近做了一个下载pdf文档的需求,本以为使用HTML5中<a>标签的属性download就能简单搞定,不料IE竟然不支持这一简单粗暴的H5新特性,而是直接在网页中打开, 于是各种搜索之后得 ...
- 项目中的那些事---PHP函数
总结工作中遇到的php函数: 1.查找:strpos("str", "substr"): 查找substr字符串在str字符串中出现的位置 第一个参数是:被查找 ...
- mui项目中如何使用原生JavaScript代替jquery来操作dom 转自【B5教程网】:http://www.bcty365.com/content-146-3661-1.html
最近在用mui写页面,当然了在移动App里引入jq或zepto这些框架,肯定是极不理性的.原生JS挺简单,为何需要jq?jq的成功当时是因为ie6.7.8.9.10.chrome.ff这些浏览器不兼容 ...
- mui项目中如何使用原生JavaScript代替jquery来操作dom
最近在用mui写页面,当然了在移动App里引入jq或zepto这些框架,肯定是极不理性的.原生JS挺简单,为何需要jq?jq的成功当时是因为ie6.7.8.9.10.chrome.ff这些浏览器不兼容 ...
- Jquery和Javascript 实际项目中写法基础-弹出窗和弹出层 (4)
一.实际项目中有很多如下界面效果. 二.该效果可以归结为弹出窗或者弹出层来实现的,为什么这么说?看如下代码: <!DOCTYPE html> <html> & ...
随机推荐
- 从零开始学android开发- 应用程序窗体显示状态操作requestWindowFeature
我们在开发程序是经常会需要软件全屏显示.自定义标题(使用按钮等控件)和其他的需求,今天这一讲就是如何控制Android应用程序的窗体显示. 首先介绍一个重要方法那就是requestWindowFeat ...
- Java模拟登录系统抓取内容【转载】
没有看考勤的习惯,导致我的一天班白上了,都是钱啊,系统也不发个邮件通知下.... 为了避免以后还有类似状况特别写了个java模拟登录抓取考勤内容的方法(部分代码来自网络),希望有人修改后也可以 ...
- Codeforces Gym 100610 Problem A. Alien Communication Masterclass 构造
Problem A. Alien Communication Masterclass Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codefo ...
- 使用CKEditor编辑器进行文本编辑
首先下载CKEditor. 下载地址:点击打开链接 将下载完的CKEditor解压而且导入到项目中. 然后在页面引入CKEditor <script type="text/javasc ...
- delphi 05 图片和超链接
超链接 /取消超链接 插入/取消 书签 插入图片 粘贴图上CTRL+v 截图 插入表情GIF WEB背景色 WEB背景图片 WebBrowser1.OleObject.document.ge ...
- springmvc03 非注解和注解处理器映射器和适配器
1其它非注解处理器映射器和适配器 .1BeanNameUrlHandlerMapping(映射器) 根据请求url(XXXX.action)匹配spring容器bean的 name 找到对应的bean ...
- C++的64位整数
在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647.而unsig ...
- Helpers\Pagination
Helpers\Pagination Break recordset into a series of pages. First create a new instance of the class ...
- VIM标记 mark 详解
转载:http://blog.163.com/lgh_2002/blog/static/44017526201081154512135/ 我的vim配置:http://pan.baidu.com/s/ ...
- Number Game poj1143
Description Christine and Matt are playing an exciting game they just invented: the Number Game. The ...