JQUERY 要给 ID 为 KK 的 input 复制的话 , 只要 : $('#KK').val('xx');
就可以了 , 我写了一段代码为了模仿 JQUERY 的效果 , 但只能只能做到
$('KK').value = , 我想做到 $('KK').val();
的地步要怎么做?我把我的代码发上来 ,请各位大师提携指点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js</title>
<script type="text/javascript">
(function(){  var y = new Function();
  y.prototype.getvid = function(id){
    return document.getElementById(id);
  }  window.$ = function(id){
    s = new y;
    return s.getvid(id);
  };
})();
</script>
</head>
<body>
<input type="text" id='i' value="test" />
<button  onclick="alert($('i').value)">获取文本框的值</button>
</body>
<script type="text/javascript">
  alert($('i').value);
</script>
</html>我连 HTML 也发上来了 ,位的就是直接复制就能用 ,我非常想实现以上效果 ,在线等 ,如果解决了 ,我有多少分,就追加多少分

解决方案 »

  1.   

    没看懂你说的意思 难道是这?<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>js</title>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"></script>
    <script type="text/javascript">
    $(function(){$("button").click(function(){alert($("#i").val());})})
    </script>
    </head>
    <body>
    <input type="text" id='i' value="test" />
    <button>获取文本框的值</button>
    </body>
    </html>
      

  2.   

    。。不是 我不要 JQUERY(function(){  var y = new Function();
      y.prototype.getvid = function(id){
      return document.getElementById(id);
      }  window.$ = function(id){
      s = new y;
      return s.getvid(id);
      };
    })();自己先写个命名空间出来 , 完了这个可以实现 $('id') , 这个就等同于 document.getElementById('id') , 所以 $('id').value = 'xx' 就可以改变 INPUT 的 VALUE ,现在我想跟着实现这一步 :
    $('id').val('xx') , 这样赋值 , 我要咋个自己写这个库?或者在我这上面拓展怎么拓展呢?或者从写?
      

  3.   

    建议直接阅读Jquery代码 这是一个简单实现的jQuery核心部分。
    http://blog.csdn.net/sencha_android/article/details/6699395window.$ = function(id){
      s = new y;
      return s.getvid(id);
    }像你这样,$返回的还是寻找元素的Dom,一点都没有包装过,当然是不可能就实现了.val(),你说你实现了.value那是Dom元素本来就有的属性,没有什么奇特之处。
      

  4.   

    简单实现,请参考 function UI(obj){
         this.self = obj;
        }
        
        UI.prototype.value = function(val){
         if (arguments.length){
         this.self.value = val;
         return this;
         } else {   
         return this.self.value;
         }  
        
        };
        
    window.$ = function(id){
    return new UI(document.getElementById(id))
    }

    $("xx").value("123");
      

  5.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Ϟ±덢τµµ</title>
    <script type="text/javascript">
    window.$ = window.jQuery 
    = function (selector) {
    return new init(selector);
    };
    function init(selector) { var elem = document.getElementById(selector);
    if (elem) {
    this.length = 1;
    this[0] = elem;
    }
    return this;
    }init.prototype = { 
    length: 0,
    size:function(){
    return this.length;
    },
    val:function(newvalue){
       if(newvalue){
        this[0].value=newvalue;
      }
      else
       return this[0].value;}}
    </script></head><body>
    <input id="YY" name="xxx" value="mmm" />
    </body>
    <script type="text/javascript">
    alert($("YY").val("cccc"));
    </script>
    </html>自己写的代码,谈不上优化,楼主所说的功能实现了。具体扩展见代码