JavaScript教程:JS中创建新对象范例

2016-02-20 00:36 1 1 收藏

想要天天向上,就要懂得享受学习。图老师为大家推荐JavaScript教程:JS中创建新对象范例,精彩的内容需要你们用心的阅读。还在等什么快点来看看吧!

【 tulaoshi.com - Web开发 】

使用JavaScript可以创建自己的对象。虽然JavaScript内部和浏览器本身的功能已十分强大,但JavaScript还是提供了创建一个新对象的方法。

使其不必像超文本标识语言那样,求于或其它多媒体工具,就能完成许多复杂的工作。

在JavaScript中创建一个新的对象是十分简单的。首先它必须定义一个对象,而后再为该对象创建一个实例。这个实例就是一个新对象,它具有对象定义中的基本特征。

五、范例

 范例1:一个动态文字滚动的例子。

test5_1.htm

html

head

title/title

script LANGUAGE="JavaScript"

with (top.window.location)

{baseURL = href.substring (0,href.lastIndexOf ("/") + 1)}

total_toc_items = 0;

current_overID = "";

last_overID = "";

browser = navigator.appName;

version = parseInt(navigator.appVersion);

client=null;

loaded = 0;

if (browser == "Netscape" && version = 3) client = "ns3";

function toc_item (img_name,icon_col,width,height) {

if (client =="ns3") {

img_prefix = baseURL + img_name;

this.icon_col = icon_col;

this.toc_img_off = new Image (width,height);

this.toc_img_off.src = img_prefix + "_off.gif";

this.toc_img_on = new Image (width,height);

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

this.toc_img_on.src = img_prefix + "_on.gif";

}

}

function new_toc_item (img_name,icon_row,width,height) {

toc_item [img_name] = new toc_item (img_name,icon_row,width,height);

}

function toc_mouseover (itemID) {

if (client =="ns3") {

current_overID = itemID;

if (current_overID != last_overID) {

document [current_overID].src = toc_item [current_overID].toc_img_on.src;

if (last_overID != "") {

document.images [last_overID].src = toc_item[last_overID].toc_img_off.src;

}

last_overID = current_overID;

}

}

}

function toc_mouseout () {

if (client =="ns3") {

if (current_overID != "") {

document.images [current_overID].src = toc_item [current_overID].toc_img_off.src;

}

current_overID = "";

last_overID = "";

}

}

new_toc_item ("1",2,120,20);

!-- Begin

function bannerObject(p){

this.msg = MESSAGE

this.out = " "

this.pos = POSITION

this.delay = DELAY

this.i = 0

this.reset = clearMessage}

function clearMessage(){

this.pos = POSITION}

var POSITION = 50;

var DELAY = 150;

var MESSAGE = "这是一个动态JavaScript文字显示的例子";

var scroll = new bannerObject();

function scroller(){

scroll.out += " ";

if(scroll.pos0)

for (scroll.i = 0; scroll.i scroll.pos; scroll.i++) { scroll.out +=" " ; }

if (scroll.pos= 0)

scroll.out += scroll.msg

else

scroll.out = scroll.msg.substring(-scroll.pos,scroll.msg.length)

document.noticeForm.notice.value = scroll.out

scroll.out = " ";

scroll.pos--;

scroll.pos--;

if (scroll.pos -(scroll.msg.length)) { scroll.reset(); } setTimeout

('scroller()',scroll.delay);}

/script

/head

body onload="scroller()" bgcolor="#000000" link="#C0C0C0" vlink="#C0C0C0"

alink="#008080"

text="#C0C0C0"

table border="0" cellspacing="0" cellpadding="0"

tr

td width="100%"form NAME="noticeForm"

pinput TYPE="text" name="notice" size="40" style="background-color: rgb(192,192,192)"/p

/form

/td

/tr

/table

/center/div

/body

/html

范例2:颜色变化的例子。

test5_2.htm

html

head

script

!--

function makearray(n) {

this.length = n;

for(var i = 1; i = n; i++)

this[i] = 0;

return this;}

hexa = new makearray(16);

for(var i = 0; i 10; i++)

hexa[i] = i;

hexa[10]="a";

hexa[11]="b";

hexa[12]="c";

hexa[13]="d";

hexa[14]="e";

hexa[15]="f";

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

function hex(i) {

if (i 0)

return "00";

else if (i 255)

return "ff";

else return "" + hexa[Math.floor(i/16)] + hexa[i%16];}

function setbgColor(r, g, b) {

var hr = hex(r);

var hg = hex(g);

var hb = hex(b);

document.bgColor = "#"+hr+hg+hb;}

function fade(sr, sg, sb, er, eg, eb, step) {

for(var i = 0; i = step; i++) {

setbgColor( Math.floor(sr * ((step-i)/step) + er * (i/step)),

Math.floor(sg * ((step-i)/step) + eg * (i/step)), Math.floor(sb *

((step-i)/step) + eb * (i/step))); }}

function fadein() {

fade(255,0,0,0,0,255,100);

fade(0,0,255,0,255,0,100);

fade(0,255,0, 0,0,0, 100);}

fadein();

// --

/script

body

/body

/html

本讲介绍了用户自行创建对象的方法, 用户可根据需要创建自己的对象。并介绍了JavaScript中建数组的方法。

来源:https://www.tulaoshi.com/n/20160220/1632229.html

延伸阅读
标签: Web开发
anchors[]; links[]; Link 连接对象 用法:document.anchors[[x]]; document.links[[x]]; anchorId; linkId document.anchors 是一个数组,包含了文档中所有锚标记(包含 name 属性的a标记),按照在文档中的次序,从 0 开始给每个锚标记定义了一个下标。 document.links&nb...
标签: Web开发
JavaScript中small函数方法是将 HTML 的SMALL 标识添加到String 对象中的文本两端。使用方法: strVariable.small( ) "String Literal".small( ) 下面的示例演示了 JavaScript中small函数方法是如何使用的: var strVariable = "This is a string"; strVariable = strVariable.small( );   ...
标签: Web开发
公司要求我在一个办公系统上加上提示功能, 我研究了很久,尝试了很多种方法.使用window.open来定时提示, 可是不过多久,客户就不耐烦得说,弹出窗口太繁了.于是我想到了用CreatePopup()的方法,不过问题来了,既然是一个办公系统,要考虑到兼容性问题,不考虑到网景浏览器,我们得考虑 IE 5.0 和 IE 5.5的问题.IE 5.0不支持CreatePopup的方法,还是只...
标签: Web开发
要实现动态交互,必须掌握有关窗体对象(Form)和框架对象(Frames)更为复杂的知识。 二、窗体中的基本元素 窗体中的基本元素由按钮、单选按钮、复选按钮、提交按钮、重置按钮、文本框等组成。 在JavaScript中要访问这些基本元素,必须通过对应特定的窗体元素的数组下标或窗体元素名来实现。每一个元素主要是通过该元素的属性或方法来引用。...
标签: Web开发
From: JavaEye.com 注意JavaScript中对象类别的定义,使用function来定义对象类别,初始化对象使用new操作符 function Person(name, age) {  this.name = name;  this.age = age;  this.toString = function() {  document.writeln("[name]:"+this.name+"br...

经验教程

28

收藏

78
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部