您的位置:寻梦网首页编程乐园CSS 初步动态HTML初步
动态HTML初步

Rewrite Layer Content 变换层次中的内容

当你用 <DIV> 设立一个层次以后, 你可以把它放早屏幕上的任何一个地方。但有没有办法更换里面的内容呢? 这一节就是介绍如何更换层次里面的内容。

Internet Explorer 4.0:

用 Explorer 很容易实现层次内容的更换。下面的语句就可以让你随意的更换层次里的内容。

    document.all.divID.innerHTML = "任何HTML或文字"

这个语句中的 divID 是你想要更换内容的 DIV 的 ID。 另外,你可以用另一种方法实现同样的功能

  document.all["divID"].innerHTML = "任何HTML或文字"
而第二种方法更方便于我们写通用的网页。

Netscape Navigator 4.0:

用 Netscape 就稍微麻烦点。 每个层次在 Netscape 里本身就是一个document.所有说用 document.write() 命令就能把你想要写的东西写到层次里。 但注意要用document.open() 和 document.close() 打开和关闭层次。比如往网页里写东西

  document.open()
  document.write("my text here")
  document.close()

如果要往层次里写东西,要加上层次的 ID

  document.layers["divID"].document.open()
  document.layers["divID"].document.write("my text here")
  document.layers["divID"].document.close()

通用程序

把上面所讨论的合在一起,我们可以写出一个通用的改换内容的子程序

function layerWrite(id,nestref,text) {
  if (ns4) {
    var lyr;
   
    if (nestref) {
      lyr = eval('document.'+nestref+'.document.'+id+'.document')
    else
      document.layers[id].document

    lyr.open()
    lyr.write(text)
    lyr.close()
  }
  else if (ie4) {
    document.all[id].innerHTML = text
  }
}

在你的网页里用这个子程序就可以更换内容。

  layerWrite("mylayer",null,"my text here")

我将会利用这个层次内容更换的功能写一个网页扩展的子程序集。 请稍候。