首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

求指教一个js有关问题

2012-11-06 
求指教一个js问题大家帮我看看onclick为什么没有效果,分别什么意思啊html xmlnshttp://www.w3.org/1999

求指教一个js问题
大家帮我看看onclick为什么没有效果,分别什么意思啊
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
  <script type="text/javascript">
  function test() {
  this.value = 1;
  this.get = function () { alert(this.value); }
  }
  var t = new test; 
  document.getElementById("a").onclick = function () {t.get() };
  document.getElementById("b").onclick= t.get;
  </script>

</head>
<body>
  <form id="form1" runat="server">
  <div>
  <input type="text" id="a" value="a" />
  <input type="text" id="b" value="b" /> 
  </div>
  </form>
</body>
</html>


[解决办法]
执行getElementById方法时,两个文本框还没有出现,所以取不到文本框。改成:

HTML code
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">  <title></title>  <script type="text/javascript">  function test() {  this.value = 1;  this.get = function () { alert(this.value); }  }  </script></head><body>  <form id="form1" runat="server">  <div>  <input type="text" id="a" value="a" />  <input type="text" id="b" value="b" />    </div><script>  var t = new test;    document.getElementById("a").onclick = function () {t.get() };  document.getElementById("b").onclick= t.get;</script>  </form></body></html>
[解决办法]
你可以看下这个精华贴:点击
[解决办法]
var t = new test; //t为test方法
document.getElementById("a").onclick = function () {t.get() };//点击后,执行t.get()脚本
document.getElementById("b").onclick= t.get;//取get的值

热点排行