项目中的那些事---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> & ...
随机推荐
- StructureMap 学习笔记(1)
前言 一个偶然的机会接触到了StructureMap,当时客户要求让程序具有较好的测试性,自然而然就想到了IOC 容器. 之后就去Google了一下, 不经意间在StackOverFlow找到一篇帖子 ...
- Python标准库:迭代器Itertools
Infinite Iterators: Iterator Arguments Results Example count() start, [step] start, start+step, star ...
- Masonry+Infinite-Scroll实现无刷新无分页完美瀑布流(转)
一.Masonry 是基于Jquery插件,用于对CSS布局的可移动层进行重新布局.Masonry愿意石工,可以这样形象的理解,页面上很多大小不一的移动层可以想象成散乱的石头,经过Masonry这个石 ...
- Ruby on Rails Tutorial 第一章 之 搭建开发环境
云端开发环境,Cloud9(https://ide.c9.io/).这个开发环境预先安装好了Rails开发所需要的大多数软件,包括Ruby.RubyGems和Git,需要自己安装Rails. 1.安装 ...
- 如何用jquery操作table的方法
今天我在做你约我吧交友www.niyuewo.com网项目时遇到一个问题,就是如何用qjuery控制table的添加.编辑与删除,经过网上查资料发现用jquery很容易实现,在此整理下来供大家参考: ...
- 将Eclipse中现有的java类生成类图
需求:将Eclipse中现有的java类生成类图 一:什么是ModelGoon? 它是一个Eclipse插件,用于基于UML图的模型设计,以及逆向工程(即从已有源代码生成类图). 二:安装 下载Mod ...
- hibernate 创建session
//1. 创建一个 SessionFactory 对象 SessionFactory sessionFactory = null; //1). 创建 Configuration 对象: 对应 hibe ...
- ubuntu14_gtk 安装
1:apt-get install build-essential2:apt-get install gnome-devel gnome-devel-docs
- LeetCode 231
Power of Two Given an integer, write a function to determine if it is a power of two. /************* ...
- [改善Java代码] 提倡异常的封装
JavaAPI提供的异常都是比较低级别的,低级别是指只有开发人员才能看懂的异常.而对于终端用户来说基本上就是天书,与业务无关,是纯计算机语言的描述. 异常封装的三方面的好处: 1)提高系统的友好性 ...