从javascript动态生成的DIV位置问题

从javascript动态生成的DIV位置问题,javascript,forms,Javascript,Forms,在这里,我从javascript生成一个div,并定位在文本框上 我发现了两个问题 1) 我的代码只适用于IE。 2) div中的文本未垂直居中放置 这是我的密码 <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Untitled Page</title> <script type="text/jav

在这里,我从javascript生成一个div,并定位在文本框上

我发现了两个问题 1) 我的代码只适用于IE。 2) div中的文本未垂直居中放置

这是我的密码

<html xmlns="http://www.w3.org/1999/xhtml" >  
  <head id="Head1" runat="server">  
  <title>Untitled Page</title>  
  <script type="text/javascript" language="javascript">
  var modalWindow = null;

  function drawDiv() {
    var txt = document.getElementById('TextBox1');
    var dime = new Dimension(txt);
    modalWindow = document.createElement('div');
    modalWindow.style.position = 'absolute';
    modalWindow.setAttribute("align", "center");
    modalWindow.setAttribute("vertical-align", "middle");
    modalWindow.innerHTML = '<p>hello...</p>';
    modalWindow.style.left = dime.x;
    modalWindow.style.top = dime.y;
    modalWindow.style.width = dime.w;
    modalWindow.style.height = dime.h;
    modalWindow.style.backgroundColor = '#C0C0C0';
    document.body.appendChild(modalWindow);
    return false;
  }

  function hider(whichDiv) {
    document.getElementById(modalWindow).style.display = 'none';
  }

  function Dimension(element) {
    this.x = -1;
    this.y = -1;
    this.w = 0;
    this.h = 0;
    if (element == document) {
      this.x = element.body.scrollLeft;
      this.y = element.body.scrollTop;
      this.w = element.body.clientWidth;
      this.h = element.body.clientHeight;
    }
    else if (element != null) {
      var e = element;
      var left = e.offsetLeft;
      while ((e = e.offsetParent) != null) {
        left += e.offsetLeft;
      }
      var e = element;
      var top = e.offsetTop;
      while ((e = e.offsetParent) != null) {
        top += e.offsetTop;
      }
      this.x = left;
      this.y = top;
      this.w = element.offsetWidth;
      this.h = element.offsetHeight;
    }
  }
   </script>  
</head>  
<body>  
<div>
<form id="form1" runat="server">

   <asp:TextBox ID="TextBox1" runat="server" Height="180px" Style="left: 307px; position: relative;  
       top: 264px" TextMode="MultiLine" Width="432px"></asp:TextBox>
    <asp:Button ID="Button1" runat="server"  
        Text="Button" OnClientClick=" return drawDiv()"  />  
</form>
</div> 
</body>  
</html>  ​

无标题页
var modalWindow=null;
函数drawDiv(){
var txt=document.getElementById('TextBox1');
var dime=新维度(txt);
modalWindow=document.createElement('div');
modalWindow.style.position='absolute';
setAttribute(“对齐”、“居中”);
setAttribute(“垂直对齐”、“中间”);
modalWindow.innerHTML='你好…

'; modalWindow.style.left=dime.x; modalWindow.style.top=尺寸y; modalWindow.style.width=尺寸w; modalWindow.style.height=尺寸h; modalWindow.style.backgroundColor='#c0'; document.body.appendChild(modalWindow); 返回false; } 函数隐藏器(whichDiv){ document.getElementById(modalWindow.style.display='none'; } 功能维度(元素){ 这个.x=-1; y=-1; 这是w=0; 这个h=0; if(元素==文档){ this.x=element.body.scrollLeft; this.y=element.body.scrollTop; this.w=element.body.clientWidth; this.h=element.body.clientHeight; } else if(元素!=null){ var e=元素; var left=e.offsetLeft; while((e=e.offsetParent)!=null){ 左+=东偏左; } var e=元素; var top=e.offsetTop; while((e=e.offsetParent)!=null){ top+=e.offsetTop; } 这个.x=左; 这个。y=顶部; this.w=element.offsetWidth; this.h=element.offsetHeight; } } ​
基本上,我试图在多行文本框上放置一个div,以防止用户访问该多行文本框。我的代码在IE中运行良好,但在firefox中运行不正常。当我在firefox中运行代码时,div在文本框中的位置不正确。我认为我的javascript不是交叉浏览器。因此,请纠正我的javascript,使之成为交叉浏览器

谢谢你

ASP


注意:如果需要,也可以隐藏文本框。

Javascript

ASP



注意:如果需要,也可以隐藏文本框。

如果需要阻止用户访问表单元素,为什么不禁用它?为什么要阻止访问?为什么不禁用文本框或根本不使用文本框?如果需要阻止用户访问表单元素,那么为什么不禁用它?为什么要阻止访问?为什么不禁用文本框或者根本不使用文本框呢?我知道有各种方法可以阻止用户访问输入控制,但我想在上面的javascript中实现这一点。如果有人能使用我的javascript浏览器,我会很高兴。如果你想用一种丑陋的方式解决所有问题,那就这样吧。:)但是不要浪费别人的时间。@user508127:如果你想从一楼到一楼,你总是坐飞机吗?当其他(可能更有经验的)人建议你不这样做时,你为什么坚持按自己的方式做呢?我知道有各种各样的方法可以阻止用户访问输入控制,但我想用上面的javascript来做。如果有人能使用我的javascript浏览器,我会很高兴。如果你想用一种丑陋的方式解决所有问题,那就这样吧。:)但是不要浪费别人的时间。@user508127:如果你想从一楼到一楼,你总是坐飞机吗?当其他人(可能更有经验)建议你不要这样做时,你为什么坚持按自己的方式做?
function disable(id) {
  var txt = document.getElementById(id);
  txt.disabled = true;
  return false;
}
<asp:Button ID="Button1" runat="server" Text="Button"
     OnClientClick="return disable('TextBox1')"  />