javascript 字符串处理
一、声明字符串:
var normal_monkey = "I am a monkey!<br>";
document.writeln("Normal monkey " + normal_monkey);
var bold_monkey = normal_monkey.bold();
document.writeln("Bold monkey " + bold_monkey);
这里的声明: var bold_monkey = normal_monkey.bold();
和下面对声明是等同的:
var bold_monkey = "<b>" + normal_monkey + "</b>";
第1个版本的声明看起来要简明得多。这里用到了字符串对象中
的bold对象,其他的字符串对象还有indexOf, charAt,
substring, 以及split, 这些方法可以深入字符串的组成结构。
首先我们研究一下indexOf。
2、indexOf
indexOf用于发现一系列的字符在一个字符串中等位置并告诉你子字符串的起始位置。如
果一个字符串中部包含该子字符串则indexOf返回returns "-1."
例子:
var the_word = "monkey";
//让我们从单词 "monkey"开始。
var location_of_m = the_word.indexOf("m");
//location_of_m(字母m的位置)将为0,因为字母m位于该字符串的起始位置。
var location_of_o = the_word.indexOf("o");
//location_of_o(字母o的位置)将为1。
var location_of_key = the_word.indexOf("key");
//location_of_key(key的位置)将为3因为子字符串“key”以字母k开始,而k
在单词monkey中的位置是3。
var location_of_y = the_word.indexOf("y");
//location_of_y)字母y的位置)是5。
var cheeky = the_word.indexOf("q");
//cheeky值是-1,因为在单词“monkey”中没有字母q。
indexOf更实用之处:
var the_email = prompt("What’s your email address?", "");
var the_at_is_at = the_email.indexOf("@");
if (the_at_is_at&nbs
相关文档:
(一)对象冒充
function A(name){
this.name = name;
this.sayHello = function(){alert(this.name+” say Hello!”);};
}
function B(name,id){
this.temp = A;
this.temp(name); &nbs ......
javascript图片浏览器的核心——图片预加载
2009-04-06 10:57
网站开发时经常需要在某个页面需要实现对大量图片的浏览,如果考虑流量的话,大可以像pconline一样每个页面只显示一张图片,让用户每看一张图片就需要重新下载一下整个页面。不过,在web2.0时代,更多人愿意用javas ......
<SCRIPT LANGUAGE="javascript">
<!--
window.open ('page.html', 'newwindow', 'height=100, width=400, top=0,left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')
file://写/成一行
-->
</SCRIPT>
参数解释:
<SCRIPT LANGUAGE="javascript"&g ......
javascript中replace()
在javascript中,String的函数replace()简直太让人喜爱了。它灵活而强大的字符替换处理能力,让我不禁想向大家介绍它。
replace()最简单的算是能力就是简单的字符替换。示例代码如下:
<script language="javascript">
var strM = "javascript is a good script ......
今天头儿复查代码,结果发现有的页面并没有相应的DOM元素,导致调用fuction出错。采用JavaScript中arguments对象可以很轻松的解决这个问题,而不需要再去判断元素之类的。so Good!
JavaScript中arguments函数对象是该对象代表正在执行的函数和调用它的函数的参数。使用方法:
[function.]arguments[n ]
其中function是 ......