今天使用 JS 写了段 jq 中的 html() 方法. 我的原则是废话不多说, 直接放代码..
- <!DOCTYPE HTML>
- <HTML lang="en">
- <head>
- <meta charset="UTF-8">
- <title>
- jQuery 初探
- </title>
- </head>
- <body>
- <div id="box">
- <span>
- 1112
- </span>
- </div>
- <script>
- var Kodo = function(selector) {
- return new Kodo.prototype.init(selector);
- }
- Kodo.prototype = {
- constructor: Kodo,
- length: 0,
- splice: [].splice,
- selector: '',
- id: {},
- init: function(selector) {
- // 这里统一认为是 id
- if (selector) {
- this.id = this.createrEleById(selector.substring(1, selector.length));
- }
- },
- HTML: function() {
- return this.toTrim(this.id.innerHTML);
- },
- createrEleById: function(idName) {
- return document.getElementById(idName);
- },
- toTrim: function(str) {
- // 去除字符串两端空格
- return str.replace(/(^\s*)|(\s*$)/g, "");
- }
- }
- Kodo.prototype.init.prototype = Kodo.prototype;
- Kodo.prototype.Ajax = function() {
- console.log('这里可以写 ajax');
- }
- Windows.$ = Kodo;
- //$('#box').Ajax();
- console.log($('#box').HTML());
- </script>
- </body>
- </HTML>
来源: http://www.bubuko.com/infodetail-2922962.html