日期:2014-05-17  浏览次数:20462 次

求指教一个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的值