Layer Writing 翻译: QQlan
层里的文字(文本或者HTML)是可以被重写的. 大家都知道IE具有可以改变在DIV标签里面的内容的能力, 当然, 你也可以在Netscape里实现类似的功能-- 使用document.write重写层内的内容.
Internet Explorer 4.0 在Explorer中, 你能成功的插入HTML到DIV标签中.
document.all.divID.innerHTML = "your new text" divID 是指DIV的ID名. 下面是我个人比较喜欢的方式:
document.all["divID"].innerHTML = "your new text" 把"divID"看成string来处理, 能增加跨browser的能力.
Netscape NAvigator 4.0 由于每个层都是独立文件, 它能做比只有一个主文件更多的事. 这对于我们今天的教程, 重写层很重要.
现在写一个re-write层的代码, 使用document.write() 命令开始书写文本, 使用document.close() 结束进程.
document.open()
document.write("my text here")
document.close()
对于由CSS定义的层, 你可以在document.write()和document.close()之间使用Netscape 的 Layers() 阵列:
document.layers["divID"].document.open()
document.layers["divID"].document.write("my text here")
document.layers["divID"].document.close()
整合要把他们非常漂亮的整合到一个generic function, IE和Netscape之间还是有些语法差异.
function layerWrite(id,nestref,text) {
if (ns4) {
var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document
lyr.open()
lyr.write(text)
lyr.close()
}
else if (ie4) document.all[id].innerHTML = text
}
使用这个function你要做的只是说明哪个层需要改变,以及文本需要做哪些改变.
layerWrite("mylayer",null,"my text here") 查看layer-writing 范例:
www.dansteinman.com/dynduo/examples/writing1-function.html--------------------------------------------------------------
5DMedia 版权所有, 转载请注明出处!