javascript 字符串处理全攻略
文章分类:
JavaScript
文章标题:
javascript 字符串处理全攻略
关 键 字:
0
文章作者:
alonglee
文章来源:
http://lmgq.vip.sina.com/tech/jsadvancedlesson/c2p1.htm
发表时间:
2004-9-21 14:43:00
一、声明字符串:
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,因为在单词“monk
相关文档:
scrollHeight: 获取对象的滚动高度。
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollWidth:获取对象的滚动宽度
offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度
offsetL ......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js除去字符串空格 ......
这个系列文章主要是讲述实现Javascript拖拽功能的基础知识,并将在最后给出一个完整的示例。适合对拖拽完全不懂的人阅读。
第一篇就先讲讲Javascript中的offsetParent属性吧。
支持的浏览器:Internet Explorer 4.0+,Mozilla 1.0+,Netscape 6.0+,Opera 7.0+,Safari ......
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Wanna tell her - interactive DHTML </title>
<meta http-equiv="imagetoolbar" content="no">
<style type="text/css">
html {
overflow: h ......