/KitJs/src/js/suger.js

https://github.com/wuxq/KitJs · JavaScript · 1652 lines · 1121 code · 0 blank · 531 comment · 325 complexity · feef07bc6494cf109858d19e75458cb1 MD5 · raw file

  1. /**
  2. * kitjs语法糖
  3. * 纯链式结构,jQuery API 100%实现
  4. * @class $Kit.Suger
  5. * @requires kit.js
  6. * @requires array.js
  7. * @requires anim.js
  8. * @requires dom.js
  9. * @requires event.js
  10. * @requires json.js
  11. * @see <a href="https://github.com/xueduany/KitJs/blob/master/KitJs/src/js/suger.js">Source code</a>
  12. * @property {[Element]} nodes Element Array List
  13. * @property {Number} length Element Array List Length
  14. */
  15. $Kit.Suger = function() {
  16. var selector = arguments[0];
  17. var context = arguments[1];
  18. if(this.isSuger(context)) {
  19. context = context.nodes[0];
  20. }
  21. if(context != null) {
  22. this.nodes = $kit.$el(selector, context);
  23. } else {
  24. if($kit.isStr(selector)) {
  25. var canHTML = false;
  26. if(selector.indexOf('<') == 0) {
  27. try {
  28. var html = $kit.newHTML(selector).childNodes;
  29. this.nodes = $kit.array.clone(html);
  30. canHTML = true;
  31. } catch(e) {
  32. }
  33. }
  34. if(!canHTML) {
  35. this.nodes = $kit.$el(selector);
  36. }
  37. } else if($kit.isNode(selector)) {
  38. this.nodes = [selector];
  39. } else if($kit.isNodeList(selector)) {
  40. this.nodes = $kit.array.clone(selector);
  41. } else if(this.isSuger(selector)) {
  42. this.nodes = $kit.array.clone(selector.nodes);
  43. }
  44. }
  45. this.length = this.nodes.length;
  46. this.name = 'kitSuger';
  47. }
  48. $kit.merge($Kit.Suger.prototype,
  49. /**
  50. * @lends $Kit.Suger.prototype
  51. */
  52. {
  53. _new : function(selector) {
  54. var _suger = new $Kit.Suger(selector);
  55. _suger.previousSugerObject = this;
  56. return _suger;
  57. },
  58. /**
  59. * 给已匹配的nodelist加点料
  60. * Add elements to the set of matched elements.
  61. * @param {Selector|Element|[Element,Element,Element ...]|HTML|$Kit.Suger Instance Object} selector
  62. * @param {Context} [context]
  63. * @return {Object} current $Kit.Suger Instance
  64. * @see <a href="http://api.jquery.com/add/">详细看jQuery的解释,和他一样</a>
  65. */
  66. add : function() {
  67. var me = this;
  68. var object = arguments[0];
  69. var context = arguments[1] || document;
  70. var re = $kit.array.clone(me.nodes);
  71. if($kit.isStr(object)) {
  72. var addNode = $kit.$el(object, context);
  73. if(addNode == null || addNode.length == 0) {
  74. addNode = $kit.newHTML(object).childNodes;
  75. }
  76. if(addNode != null && addNode.length) {
  77. $kit.array.ad(re, addNode, {
  78. ifExisted : true
  79. });
  80. }
  81. } else if($kit.isNode(object)) {
  82. re.push(object);
  83. } else if($kit.isNodeList(object)) {
  84. $kit.array.ad(re, object, {
  85. ifExisted : true
  86. });
  87. } else if(me.isSuger(object)) {
  88. $kit.array.ad(re, object.nodes, {
  89. ifExisted : true
  90. });
  91. }
  92. return me._new(re);
  93. },
  94. /**
  95. * 你懂得,添加样式
  96. * Adds the specified class(es) to each of the set of matched elements.
  97. * @param {String|Function} className
  98. * @return {Object} current $Kit.Suger Instance
  99. * @see <a href="http://api.jquery.com/addClass/">详细看jQuery的解释,和他一样</a>
  100. */
  101. addClass : function() {
  102. var me = this;
  103. var className = arguments[0];
  104. if($kit.isStr(className)) {
  105. $kit.each(me.nodes, function(o) {
  106. $kit.adCls(o, className);
  107. });
  108. } else if($kit.isFn(className)) {
  109. $kit.each(me.nodes, function(o, index) {
  110. $kit.adCls(o, className.apply(o, [index]));
  111. });
  112. }
  113. return me;
  114. },
  115. /**
  116. * Remove a single class, multiple classes, or all classes from each element in the set of matched elements.
  117. * @param {String}
  118. * @return {Object} current $Kit.Suger Instance
  119. */
  120. removeClass : function(className) {
  121. var me = this;
  122. $kit.each(me.nodes, function(o) {
  123. $kit.rmCls(o, className);
  124. });
  125. return me;
  126. },
  127. /**
  128. * 切换className
  129. * Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.
  130. * @param {String|Function} className
  131. * @param {Boolean} switch add or del?
  132. * @return {Object} current $Kit.Suger Instance
  133. */
  134. toggleClass : function(className, flag) {
  135. var me = this;
  136. $kit.each(me.nodes, function(o) {
  137. if($kit.isFn(className)) {
  138. className = className.call(o, index);
  139. }
  140. if(flag == null) {
  141. $kit.toggleCls(o, className);
  142. } else {
  143. if(flag == true) {
  144. $kit.adCls(o, className);
  145. } else if(flag == false) {
  146. $kit.rmCls(o, className);
  147. }
  148. }
  149. });
  150. return me;
  151. },
  152. /**
  153. * 是否有某个样式
  154. * Determine whether any of the matched elements are assigned the given class.
  155. * @param {String}
  156. * @return {Boolean}
  157. */
  158. hasClass : function(className) {
  159. var me = this;
  160. return $kit.hsCls(me.nodes[0], className);
  161. },
  162. /**
  163. * 将上一次调用的nodelist加到当前的nodelist里面
  164. * Add the previous set of elements on the stack to the current set.
  165. * @return {Object} new $Kit.Suger Instance
  166. * @see <a href="http://api.jquery.com/andSelf/">详细看jQuery的解释,和他一样</a>
  167. */
  168. andSelf : function() {
  169. var me = this;
  170. if(me.previousSugerObject) {
  171. var re = me.previousSugerObject;
  172. $kit.array.ad(re.nodes, me.nodes);
  173. return re;
  174. }
  175. return me;
  176. },
  177. /**
  178. * 在previousSibling插入元素
  179. * Insert content, specified by the parameter, before each element in the set of matched elements.
  180. * @see <a href="http://api.jquery.com/before/">详细看jQuery的解释,和他一样</a>
  181. * @param {HTML|Element|[Element]|$Kit.Suger Instance Object|Function} content
  182. * @return {Object} current $Kit.Suger Instance
  183. */
  184. before : function() {
  185. var me = this;
  186. if(arguments.length) {
  187. for(var i = 0; i < arguments.length; i++) {
  188. me.after(arguments[i]);
  189. }
  190. } else {
  191. var object = arguments[0];
  192. if($kit.isNode(object)) {
  193. $kit.each(me.nodes, function(o) {
  194. $kit.insEl({
  195. where : o,
  196. pos : 'before',
  197. what : object
  198. });
  199. });
  200. } else if($kit.isNodeList(object)) {
  201. $kit.each(me.nodes, function(o) {
  202. $kit.each(object, function(o1) {
  203. $kit.insEl({
  204. where : o,
  205. pos : 'before',
  206. what : o1
  207. });
  208. });
  209. });
  210. } else if(me.isSuger(object)) {
  211. $kit.each(me.nodes, function(o) {
  212. $kit.each(object.nodes, function(o1) {
  213. $kit.insEl({
  214. where : o,
  215. pos : 'before',
  216. what : o1
  217. });
  218. });
  219. });
  220. } else if($kit.isFn(object)) {
  221. $kit.each(me.nodes, function(o, index) {
  222. me.after(object.call(object, index));
  223. });
  224. }
  225. }
  226. return me;
  227. },
  228. /**
  229. * 当前对象的nextSibling插入一个神马东东
  230. * Insert content, specified by the parameter, after each element in the set of matched elements.
  231. * @see <a href="http://api.jquery.com/after/">详细看jQuery的解释,和他一样</a>
  232. * @return {Object} current $Kit.Suger Instance
  233. * @param {HTML|Element|[Element]|$Kit.Suger Instance Object|Function} content
  234. */
  235. after : function() {
  236. var me = this;
  237. if(arguments.length) {
  238. for(var i = 0; i < arguments.length; i++) {
  239. me.after(arguments[i]);
  240. }
  241. } else {
  242. var object = arguments[0];
  243. if($kit.isNode(object)) {
  244. $kit.each(me.nodes, function(o) {
  245. $kit.insEl({
  246. where : o,
  247. pos : 'after',
  248. what : object
  249. });
  250. });
  251. } else if($kit.isNodeList(object)) {
  252. $kit.each(me.nodes, function(o) {
  253. $kit.each(object, function(o1) {
  254. $kit.insEl({
  255. where : o,
  256. pos : 'after',
  257. what : o1
  258. });
  259. });
  260. });
  261. } else if(me.isSuger(object)) {
  262. $kit.each(me.nodes, function(o) {
  263. $kit.each(object.nodes, function(o1) {
  264. $kit.insEl({
  265. where : o,
  266. pos : 'after',
  267. what : o1
  268. });
  269. });
  270. });
  271. } else if($kit.isFn(object)) {
  272. $kit.each(me.nodes, function(o, index) {
  273. me.after(object.call(object, index));
  274. });
  275. }
  276. }
  277. return me;
  278. },
  279. /**
  280. * 在屁股插入html
  281. * Insert content, specified by the parameter, to the end of each element in the set of matched elements.
  282. * @see <a href="http://api.jquery.com/append/">详细看jQuery的解释,和他一样</a>
  283. * @param {HTML|Element|[Element]|$Kit.Suger Instance Object|Function} content
  284. * @return {Object} current $Kit.Suger Instance
  285. */
  286. append : function() {
  287. var me = this;
  288. if(arguments.length) {
  289. for(var i = 0; i < arguments.length; i++) {
  290. me.after(arguments[i]);
  291. }
  292. } else {
  293. var object = arguments[0];
  294. if($kit.isNode(object)) {
  295. $kit.each(me.nodes, function(o) {
  296. $kit.insEl({
  297. where : o,
  298. pos : 'last',
  299. what : object
  300. });
  301. });
  302. } else if($kit.isNodeList(object)) {
  303. $kit.each(me.nodes, function(o) {
  304. $kit.each(object, function(o1) {
  305. $kit.insEl({
  306. where : o,
  307. pos : 'last',
  308. what : o1
  309. });
  310. });
  311. });
  312. } else if(me.isSuger(object)) {
  313. $kit.each(me.nodes, function(o) {
  314. $kit.each(object.nodes, function(o1) {
  315. $kit.insEl({
  316. where : o,
  317. pos : 'last',
  318. what : o1
  319. });
  320. });
  321. });
  322. } else if($kit.isFn(object)) {
  323. $kit.each(me.nodes, function(o, index) {
  324. me.after(object.call(object, index));
  325. });
  326. }
  327. }
  328. return me;
  329. },
  330. /**
  331. * 添加到谁的屁股
  332. * Insert every element in the set of matched elements to the end of the target.
  333. * @see <a href="http://api.jquery.com/appendTo/">详细看jQuery的解释,和他一样</a>
  334. * @param {Element|$Kit.Suger Instance Object}
  335. * @return {Object} current $Kit.Suger Instance
  336. */
  337. appendTo : function(target) {
  338. var me = this;
  339. if($kit.isNode(target)) {
  340. $kit.each(me.nodes, function(o) {
  341. target.appendChild(o);
  342. });
  343. } else if(me.isSuger(target)) {
  344. $kit.each(me.nodes, function(o) {
  345. target.nodes[0].appendChild(o);
  346. });
  347. }
  348. return me;
  349. },
  350. /**
  351. * 在头部插入
  352. * Insert every element in the set of matched elements before the target.
  353. * @see <a href="http://api.jquery.com/insertBefore/">详细看jQuery的解释,和他一样</a>
  354. * @param {Selector|Element||$Kit.Suger Instance Object}
  355. * @param {Element} [context]
  356. * @return {Object} current $Kit.Suger Instance
  357. */
  358. insertBefore : function(selector, context) {
  359. var me = this, target = selector;
  360. if($kit.isNode(target)) {
  361. //
  362. } else if($kit.isSuger(target)) {
  363. target = target.nodes[0];
  364. } else if($kit.isStr(target)) {
  365. target = $kit.$el(selector, context)[0];
  366. }
  367. $kit.each(me.nodes, function(o) {
  368. $kit.insEl({
  369. where : target,
  370. what : o,
  371. pos : 'before'
  372. });
  373. });
  374. return me;
  375. },
  376. /**
  377. * 在屁股插入
  378. * Insert every element in the set of matched elements after the target.
  379. * @see <a href="http://api.jquery.com/insertAfter/">详细看jQuery的解释,和他一样</a>
  380. * @param {Selector|Element||$Kit.Suger Instance Object}
  381. * @param {Element} [context]
  382. * @return {Object} current $Kit.Suger Instance
  383. */
  384. insertAfter : function(selector, context) {
  385. var me = this, target = selector;
  386. if($kit.isNode(target)) {
  387. //
  388. } else if($kit.isSuger(target)) {
  389. target = target.nodes[0];
  390. } else if($kit.isStr(target)) {
  391. target = $kit.$el(selector, context)[0];
  392. }
  393. $kit.each(me.nodes, function(o) {
  394. $kit.insEl({
  395. where : target,
  396. what : o,
  397. pos : 'after'
  398. });
  399. });
  400. return me;
  401. },
  402. /**
  403. * 在头部插入html
  404. * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.
  405. * @see <a href="http://api.jquery.com/prepend/">详细看jQuery的解释,和他一样</a>
  406. * @param {HTML|Element|[Element]|$Kit.Suger Instance Object|Function} content
  407. * @return {Object} current $Kit.Suger Instance
  408. */
  409. prepend : function() {
  410. var me = this;
  411. if(arguments.length) {
  412. for(var i = 0; i < arguments.length; i++) {
  413. me.after(arguments[i]);
  414. }
  415. } else {
  416. var object = arguments[0];
  417. if($kit.isNode(object)) {
  418. $kit.each(me.nodes, function(o) {
  419. $kit.insEl({
  420. where : o,
  421. pos : 'first',
  422. what : object
  423. });
  424. });
  425. } else if($kit.isNodeList(object)) {
  426. $kit.each(me.nodes, function(o) {
  427. $kit.each(object, function(o1) {
  428. $kit.insEl({
  429. where : o,
  430. pos : 'first',
  431. what : o1
  432. });
  433. });
  434. });
  435. } else if(me.isSuger(object)) {
  436. $kit.each(me.nodes, function(o) {
  437. $kit.each(object.nodes, function(o1) {
  438. $kit.insEl({
  439. where : o,
  440. pos : 'first',
  441. what : o1
  442. });
  443. });
  444. });
  445. } else if($kit.isFn(object)) {
  446. $kit.each(me.nodes, function(o, index) {
  447. me.after(object.call(object, index));
  448. });
  449. }
  450. }
  451. return me;
  452. },
  453. /**
  454. * 添加到谁的头部
  455. * Insert every element in the set of matched elements to the beginning of the target.
  456. * @see <a href="http://api.jquery.com/prependTo/">详细看jQuery的解释,和他一样</a>
  457. * @param {Element|$Kit.Suger Instance Object}
  458. * @return {Object} current $Kit.Suger Instance
  459. */
  460. prependTo : function(target) {
  461. var me = this;
  462. if($kit.isNode(target)) {
  463. $kit.each(me.nodes, function(o) {
  464. $kit.insEl({
  465. where : target,
  466. what : o,
  467. pos : 'first'
  468. });
  469. });
  470. } else if(me.isSuger(target)) {
  471. $kit.each(me.nodes, function(o) {
  472. $kit.insEl({
  473. where : target.nodes[0],
  474. what : o,
  475. pos : 'first'
  476. });
  477. });
  478. }
  479. return me;
  480. },
  481. /**
  482. * 属性设置
  483. * Get the value of an attribute for the first element in the set of matched elements.
  484. * @see <a href="http://api.jquery.com/attr/">详细看jQuery的解释,和他一样</a>
  485. * @param {String}
  486. * @param {String}
  487. * @return {Object|String} current $Kit.Suger Instance or string value
  488. */
  489. attr : function(attrName, attrValue) {
  490. var me = this;
  491. if(attrValue == null) {
  492. return $kit.attr(me.nodes[0], attrName);
  493. } else {
  494. $kit.each(me.nodes, function(o) {
  495. $kit.attr(o, attrName, attrValue);
  496. });
  497. }
  498. return me;
  499. },
  500. /**
  501. * 属性,同attr
  502. */
  503. prop : function() {
  504. return this.attr.apply(this, arguments);
  505. },
  506. /**
  507. * Remove an attribute from each element in the set of matched elements.
  508. * @param {String}
  509. * @return {Object} current $Kit.Suger Instance
  510. */
  511. removeAttr : function(attrName) {
  512. var me = this;
  513. $kit.each(me.nodes, function(o) {
  514. $kit.attr(o, attrName, null);
  515. });
  516. return me;
  517. },
  518. /**
  519. * Remove an attribute from each element in the set of matched elements.
  520. * @param {String}
  521. * @return {Object} current $Kit.Suger Instance
  522. */
  523. removeProp : function() {
  524. return this.removeAttr.apply(this, arguments);
  525. },
  526. /**
  527. * clone node
  528. * @return {Object} new $Kit.Suger Instance
  529. */
  530. clone : function() {
  531. var cloneNodes = [], me = this;
  532. $kit.each(me.nodes, function(o) {
  533. cloneNodes.push($kit.dom.clone(o));
  534. });
  535. return me._new(cloneNodes);
  536. },
  537. /**
  538. * 从自身开始查找,返回符合条件的元素
  539. * Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
  540. * @return {Object} new $Kit.Suger Instance
  541. */
  542. closest : function(selector, context) {
  543. var me = this;
  544. context = context || document;
  545. var me = this, element = me.nodes[0];
  546. if($kit.selector.matchesSelector(element, selector)) {
  547. return me._new(element);
  548. }
  549. return me.parent(selector, context);
  550. },
  551. /**
  552. * 样式
  553. * Get the value of a style property for the first element in the set of matched elements.
  554. * @return {Object|String} current $Kit.Suger Instance or string value
  555. */
  556. css : function(propertyName, value) {
  557. var me = this;
  558. $kit.css(me.nodes[0], propertyName, value);
  559. return me;
  560. },
  561. /**
  562. * 判断dom位置是否包含
  563. * @param {Element|Selector}
  564. * @param {Element} [contenxt]
  565. * @return {Boolean}
  566. */
  567. contains : function(selector, context) {
  568. var me = this, context = context || document;
  569. var compareNode = $kit.$el(selector, context);
  570. if(compareNode != null && compareNode.length) {
  571. return $kit.contains(me.nodes[0], compareNode[0]);
  572. }
  573. return false;
  574. },
  575. /**
  576. * 建立dom与js对象的关系
  577. * Store arbitrary data associated with the matched elements.
  578. * @see <a href="http://api.jquery.com/data/">详细看jQuery的解释,和他一样</a>
  579. * @param {String}
  580. * @param {String}
  581. * @return {Object} current $Kit.Suger Instance
  582. */
  583. data : function(key, value) {
  584. var me = this;
  585. if($kit.isStr(key) && value != null) {
  586. this.nodes[0][key] = value;
  587. } else if($kit.isObj(key)) {
  588. for(var p in key) {
  589. this.nodes[0][p] = key[p];
  590. }
  591. }
  592. return me;
  593. },
  594. /**
  595. * Remove a previously-stored piece of data.
  596. * @param {String}
  597. * @return {Object} current $Kit.Suger Instance
  598. */
  599. removeData : function(dataName) {
  600. var me = this;
  601. $kit.each(me.nodes, function(o) {
  602. o.dataName = undefined;
  603. });
  604. return me;
  605. },
  606. /**
  607. * 从this.nodes里面删除匹配的元素
  608. * Remove the set of matched elements from the DOM.
  609. * @see <a href="http://api.jquery.com/detach/">详细看jQuery的解释,和他一样</a>
  610. * @param {Selector}
  611. * @return {Object} new $Kit.Suger Instance
  612. */
  613. detach : function(selector) {
  614. var me = this;
  615. var nodesClone = $kit.array.clone(me.nodes);
  616. $kit.each(nodesClone, function(o) {
  617. if($kit.selector.matchesSelector(o, selector)) {
  618. o = null;
  619. }
  620. });
  621. $kit.array.delEmpty(nodesClone);
  622. return me._new(nodesClone);
  623. },
  624. /**
  625. * 遍历
  626. * Iterate over a jQuery object, executing a function for each matched element.
  627. * @param {Function} fn function(index, Element)A function to execute for each matched element.
  628. * @return {Object} current $Kit.Suger Instance
  629. * @see <a href="http://api.jquery.com/each/">详细看jQuery的解释,和他一样</a>
  630. */
  631. each : function(fn) {
  632. var me = this;
  633. $kit.each(me.nodes, function(node, index, ary) {
  634. fn.call(node, index, node);
  635. });
  636. return me;
  637. },
  638. /**
  639. * 删除childNodes
  640. * Remove all child nodes of the set of matched elements from the DOM.
  641. * @return {Object} current $Kit.Suger Instance
  642. * @see <a href="http://api.jquery.com/empty/">详细看jQuery的解释,和他一样</a>
  643. */
  644. empty : function() {
  645. var me = this;
  646. $kit.each(me.nodes, function(node, index, ary) {
  647. node.innerHTML = '';
  648. });
  649. return me;
  650. },
  651. /**
  652. * 返回前一个
  653. * End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.
  654. * @return {Object} previous $Kit.Suger Instance
  655. */
  656. end : function() {
  657. var me = this;
  658. if(me.previousSugerObject) {
  659. return me.previousSugerObject;
  660. }
  661. return null;
  662. },
  663. /**
  664. * 返回数组中第几个元素
  665. * Reduce the set of matched elements to the one at the specified index.
  666. * @param {Number}
  667. * @return {Object} new $Kit.Suger Instance
  668. * @see <a href="http://api.jquery.com/eq/">详细看jQuery的解释,和他一样</a>
  669. */
  670. eq : function(index) {
  671. var me = this;
  672. if(index >= 0) {
  673. return me._new(me.nodes[index]);
  674. }
  675. return me._new(me.nodes[me.nodes.length + index]);
  676. },
  677. /**
  678. * 过滤,生成新的
  679. * Reduce the set of matched elements to those that match the selector or pass the function's test.
  680. * @param {Selector|Element|[Element]}
  681. * @return {Object} new $Kit.Suger Instance
  682. * @see <a href="http://api.jquery.com/filter/">详细看jQuery的解释,和他一样</a>
  683. */
  684. filter : function(selector) {
  685. var me = this, re = [];
  686. var find;
  687. if($kit.isStr(selector)) {
  688. find = $kit.selector.matches(selector, me.nodes);
  689. } else if($kit.isNode(selector)) {
  690. find = [selector];
  691. } else if($kit.isNodeList(selector)) {
  692. find = selector;
  693. }
  694. $kit.each(me.nodes, function(o) {
  695. var ifExisted = false;
  696. $kit.each(find, function(c) {
  697. if(o == c) {
  698. ifExisted = true;
  699. return false;
  700. }
  701. });
  702. if(!ifExisted) {
  703. re.push(o);
  704. }
  705. });
  706. return me._new(re);
  707. },
  708. /**
  709. * 找到,生成新的
  710. * Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.
  711. * @param {Selector|Element|[Element]}
  712. * @return {Object} new $Kit.Suger Instance
  713. */
  714. find : function(selector) {
  715. var me = this;
  716. var find;
  717. if($kit.isStr(selector)) {
  718. find = $kit.selector.matches(selector, me.nodes);
  719. } else if($kit.isNode(selector)) {
  720. $kit.each(me.nodes, function(o) {
  721. if(o == selector) {
  722. find = o;
  723. return false;
  724. }
  725. });
  726. } else if($kit.isNodeList(selector)) {
  727. var re = [];
  728. $kit.each(me.nodes, function(o) {
  729. var flag = true;
  730. $kit.each(selector, function(p) {
  731. if(o == p) {
  732. $kit.array.ad(re, o, {
  733. ifExisted : true
  734. });
  735. }
  736. });
  737. });
  738. if(re.length) {
  739. find = re;
  740. }
  741. }
  742. return me._new(find);
  743. },
  744. /**
  745. * 返回第一个
  746. * Reduce the set of matched elements to the first in the set.
  747. * @return {Object} new $Kit.Suger Instance
  748. */
  749. first : function() {
  750. var me = this;
  751. return me._new(me.nodes[0]);
  752. },
  753. /**
  754. * 返回包含某种node的
  755. * Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.
  756. * @param {Selector|Element}
  757. * @see <a href="http://api.jquery.com/has/">详细看jQuery的解释,和他一样</a>
  758. */
  759. has : function(selector) {
  760. var me = this, re = [];
  761. $kit.each(me.nodes, function(o, index, ary) {
  762. if($kit.isStr(selector)) {
  763. var find = $kit.$el(selector, o);
  764. if(find != null && find.length > 0) {
  765. re.push(o);
  766. }
  767. } else if($kit.isNode(selector)) {
  768. if($kit.contains(o, selector)) {
  769. re.push(o);
  770. }
  771. }
  772. });
  773. return me._new(re);
  774. },
  775. /**
  776. * 隐藏
  777. * Hide the matched elements.
  778. */
  779. hide : function() {
  780. var me = this;
  781. me.nodes[0].style.display = 'none';
  782. return me;
  783. },
  784. /**
  785. * 显示
  786. */
  787. show : function() {
  788. var me = this;
  789. if(me.nodes[0].style.display == 'none') {
  790. me.nodes[0].style.display = '';
  791. }
  792. return me;
  793. },
  794. /**
  795. * 返回html
  796. * Get the HTML contents of the first element in the set of matched elements.
  797. * @see <a href="http://api.jquery.com/html/">详细看jQuery的解释,和他一样</a>
  798. * @param {String} [html]
  799. */
  800. html : function(html) {
  801. return $kit.dom.html(this.nodes[0], html);
  802. },
  803. /**
  804. * 返回html
  805. * Get the combined text contents of each element in the set of matched elements, including their descendants.
  806. * @see <a href="http://api.jquery.com/text/">详细看jQuery的解释,和他一样</a>
  807. * @param {String} [text]
  808. */
  809. text : function(text) {
  810. return $kit.dom.text(this.nodes[0], html);
  811. },
  812. /**
  813. * 返回索引
  814. * Search for a given element from among the matched elements.
  815. * @param {Element|Selector}
  816. * @param {Element} [context]
  817. * @return {Number}
  818. * @see <a href="http://api.jquery.com/index/">详细看jQuery的解释,和他一样</a>
  819. */
  820. index : function(selector, context) {
  821. if($kit.isNode(selector)) {
  822. return $kit.array.indexOf(this.nodes, selector);
  823. }
  824. var find = $kit.$el(selector, context);
  825. return $kit.array.indexOf(this.nodes, find[0]);
  826. },
  827. /**
  828. * 高度
  829. * @param {Number} [value]
  830. * @return {Number|$Kit.Suger Instance}
  831. */
  832. height : function(value) {
  833. var me = this;
  834. if(value != null) {
  835. $kit.dom.height(me.nodes[0], value);
  836. return me;
  837. }
  838. return $kit.dom.height(me.nodes[0]);
  839. },
  840. /**
  841. * 宽度
  842. * @param {Number} [value]
  843. * @return {Number|$Kit.Suger Instance}
  844. */
  845. width : function(value) {
  846. var me = this;
  847. if(value != null) {
  848. $kit.dom.width(me.nodes[0], value);
  849. return me;
  850. }
  851. return $kit.dom.width(me.nodes[0]);
  852. },
  853. /**
  854. * 内高度包括padding,不包括border
  855. * Get the current computed height for the first element in the set of matched elements, including padding but not border.
  856. * @see <a href="http://api.jquery.com/innerHeight/">详细看jQuery的解释,和他一样</a>
  857. */
  858. innerHeight : function() {
  859. return $kit.dom.innerHeight(this.nodes[0]);
  860. },
  861. /**
  862. * Get the current computed width for the first element in the set of matched elements, including padding but not border.
  863. * @see <a href="http://api.jquery.com/innerWidth/">详细看jQuery的解释,和他一样</a>
  864. */
  865. innerWidth : function() {
  866. return $kit.dom.innerWidth(this.nodes[0]);
  867. },
  868. /**
  869. * 包括padding和border
  870. * Get the current computed height for the first element in the set of matched elements, including padding, border, and optionally margin. Returns an integer (without "px") representation of the value or null if called on an empty set of elements.
  871. * @see <a href="http://api.jquery.com/outerHeight/">详细看jQuery的解释,和他一样</a>
  872. * @return {Number}
  873. */
  874. outerHeight : function() {
  875. return $kit.dom.outerHeight(this.nodes[0]);
  876. },
  877. /**
  878. * 包括padding和border
  879. * Get the current computed width for the first element in the set of matched elements, including padding and border.
  880. * @see <a href="http://api.jquery.com/outerWidth/">详细看jQuery的解释,和他一样</a>
  881. * @return {Number}
  882. */
  883. outerWidth : function() {
  884. return $kit.dom.outerWidth(this.nodes[0]);
  885. },
  886. /**
  887. * 判断是否满足选择器
  888. * Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.
  889. * @see <a href="http://api.jquery.com/is/">详细看jQuery的解释,和他一样</a>
  890. * @param {Selector|$Kit.Suger Instance Object|Element}
  891. */
  892. is : function(selector) {
  893. if($kit.isNode(selector)) {
  894. return this.nodes[0] == selector;
  895. } else if(this.isSuger(selector)) {
  896. return selector.nodes.length == 1 && this.nodes[0] == selector.nodes[0]
  897. } else if($kit.isStr(selector)) {
  898. return $kit.selector.matchesSelector(this.nodes[0], selector);
  899. }
  900. return false;
  901. },
  902. /**
  903. * 最后一个
  904. * Reduce the set of matched elements to the final one in the set.
  905. * @see <a href="http://api.jquery.com/last/">详细看jQuery的解释,和他一样</a>
  906. * @return {Object} new $Kit.Suger Instance
  907. */
  908. last : function() {
  909. var me = this;
  910. return me._new(me.nodes[me.nodes.length - 1]);
  911. },
  912. /**
  913. * 遍历,返回值加入到一个数组
  914. * Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.
  915. * @see <a href="http://api.jquery.com/map/">详细看jQuery的解释,和他一样</a>
  916. * @param {Function}
  917. * @return {Array}
  918. */
  919. map : function(fn) {
  920. var me = this, re = [];
  921. $kit.each(me.nodes, function(o, idx, ary) {
  922. re.push(fn.call(o, idx, o));
  923. });
  924. return re;
  925. },
  926. /**
  927. * 去掉匹配的
  928. * Remove elements from the set of matched elements.
  929. * @see <a href="http://api.jquery.com/andSelf/">详细看jQuery的解释,和他一样</a>
  930. * @return {Object} new $Kit.Suger Instance
  931. */
  932. not : function(selector, context) {
  933. var me = this, re = [];
  934. if($kit.isNode(selector)) {
  935. $kit.each(me.nodes, function(o) {
  936. if(selector != o) {
  937. re.push(o);
  938. }
  939. });
  940. return me._new(re);
  941. } else if($kit.isNodeList(selector)) {
  942. $kit.each(me.nodes, function(o) {
  943. var existed = false;
  944. $kit.each(selector, function(c) {
  945. if(o == c) {
  946. existed = true;
  947. return false;
  948. }
  949. });
  950. if(!existed) {
  951. re.push(o);
  952. }
  953. });
  954. return me._new(re);
  955. } else if($kit.isStr(selector)) {
  956. selector = $kit.$el(selector, context);
  957. $kit.each(me.nodes, function(o) {
  958. var existed = false;
  959. $kit.each(selector, function(c) {
  960. if(o == c) {
  961. existed = true;
  962. return false;
  963. }
  964. });
  965. if(!existed) {
  966. re.push(o);
  967. }
  968. });
  969. return me._new(re);
  970. } else if($kit.isFn(selector)) {
  971. $kit.each(me.nodes, function(o, idx) {
  972. if(selector.call(o, idx) != true) {
  973. re.push(o);
  974. }
  975. });
  976. }
  977. return null;
  978. },
  979. /**
  980. * 注销事件
  981. * @param {String} event
  982. * @param {Function} [eventFunction]
  983. * @return {Object} current $Kit.Suger Instance
  984. */
  985. off : function() {
  986. var me = this;
  987. if(arguments.length == 2) {
  988. var ev = arguments[0];
  989. var fn = arguments[1];
  990. $kit.each(me.nodes, function(o) {
  991. $kit.delEv({
  992. el : o,
  993. ev : ev,
  994. fn : fn
  995. });
  996. });
  997. }
  998. return me;
  999. },
  1000. /**
  1001. * 注销事件,同off
  1002. * @param {String} event
  1003. * @param {Function} [eventFunction]
  1004. * @return {Object} current $Kit.Suger Instance
  1005. */
  1006. unbind : function() {
  1007. return this.off.apply(this, arguments);
  1008. },
  1009. /**
  1010. * 注销事件,同off
  1011. * @param {String} event
  1012. * @param {Function} [eventFunction]
  1013. * @return {Object} current $Kit.Suger Instance
  1014. */
  1015. undelegate : function() {
  1016. return this.off.apply(this, arguments);
  1017. },
  1018. /**
  1019. * 绑定事件,同on
  1020. * @param {String} event
  1021. * @param {Function} eventFunction
  1022. * @return {Object} current $Kit.Suger Instance
  1023. */
  1024. bind : function() {
  1025. return this.on.apply(this, arguments);
  1026. },
  1027. /**
  1028. * 绑定事件,同on
  1029. * @param {String} event
  1030. * @param {Function} eventFunction
  1031. * @return {Object} current $Kit.Suger Instance
  1032. */
  1033. delegate : function() {
  1034. return this.on.apply(this, arguments);
  1035. },
  1036. /**
  1037. * 绑定事件
  1038. * @param {String} event
  1039. * @param {Function} eventFunction
  1040. * @return {Object} current $Kit.Suger Instance
  1041. */
  1042. on : function() {
  1043. var me = this;
  1044. if(arguments.length == 2) {
  1045. var ev = arguments[0];
  1046. var fn = arguments[1];
  1047. $kit.each(me.nodes, function(o) {
  1048. $kit.ev({
  1049. el : o,
  1050. ev : ev,
  1051. fn : fn,
  1052. scope : o
  1053. });
  1054. });
  1055. }
  1056. return me;
  1057. },
  1058. /**
  1059. * 只执行一次的事件
  1060. * @param {String} event
  1061. * @param {Function} eventFunction
  1062. * @return {Object} current $Kit.Suger Instance
  1063. */
  1064. one : function(event, fn) {
  1065. var me = this;
  1066. var _fn = function(ev, evCfg) {
  1067. fn.call(this, ev, evCfg);
  1068. $kit.delEv(evCfg);
  1069. }
  1070. me.on(event, _fn);
  1071. },
  1072. /**
  1073. * offset,看$kit.offset
  1074. * Get the current coordinates of the first element in the set of matched elements, relative to the document.
  1075. * @return {Object}
  1076. */
  1077. offset : function() {
  1078. return $kit.offset(this.nodes[0]);
  1079. },
  1080. /**
  1081. * position,看$kit.position
  1082. * Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.
  1083. * @return {Object}
  1084. */
  1085. position : function() {
  1086. return $kit.dom.position(this.nodes[0]);
  1087. },
  1088. /**
  1089. * Get the closest ancestor element that is positioned.
  1090. * @return {Object} new $Kit.Suger Instance
  1091. */
  1092. offsetParent : function() {
  1093. return this._new(this.nodes[offsetParent]);
  1094. },
  1095. /**
  1096. * 查找父节点
  1097. * Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
  1098. * @see <a href="http://api.jquery.com/parent/">详细看jQuery的解释,和他一样</a>
  1099. * @param {Selector|Element|$Kit.Suger Instance Object}
  1100. * @param {Element} [context]
  1101. * @return {Object} new $Kit.Suger Instance
  1102. */
  1103. parent : function(selector, context) {
  1104. var me = this;
  1105. context = context || document;
  1106. var me = this, element = me.nodes[0];
  1107. var parentNode = me.nodes[0];
  1108. while(parentNode != context) {
  1109. parentNode = parentNode.parentNode;
  1110. if($kit.isStr(selector) && $kit.selector.matchesSelector(parentNode, selector)) {
  1111. return me._new(parentNode);
  1112. } else if($kit.isNode(selector) && parentNode == selector) {
  1113. return me._new(parentNode);
  1114. } else if(me.isSuger(selector) && parentNode == selector.nodes[0]) {
  1115. return me._new(parentNode);
  1116. } else {
  1117. break;
  1118. }
  1119. }
  1120. return null;
  1121. },
  1122. /**
  1123. * 返回所有父节点
  1124. * Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.
  1125. * @param {Selector|Element|$Kit.Suger Instance Object}
  1126. * @param {Element} [context]
  1127. * @return {Object} new $Kit.Suger Instance
  1128. */
  1129. parents : function(selector, context) {
  1130. var me = this;
  1131. context = context || document;
  1132. var me = this, element = me.nodes[0];
  1133. var parentNode = me.nodes[0];
  1134. var re = [];
  1135. while(parentNode != context) {
  1136. parentNode = parentNode.parentNode;
  1137. if($kit.isStr(selector) && $kit.selector.matchesSelector(parentNode, selector)) {
  1138. re.push(me._new(parentNode));
  1139. } else if($kit.isNode(selector) && parentNode == selector) {
  1140. re.push(me._new(parentNode));
  1141. } else if(me.isSuger(selector) && parentNode == selector.nodes[0]) {
  1142. re.push(me._new(parentNode));
  1143. } else {
  1144. break;
  1145. }
  1146. }
  1147. if(re.length) {
  1148. return me._new(re);
  1149. }
  1150. return null;
  1151. },
  1152. /**
  1153. * 返回所有父节点直到
  1154. * Get the ancestors of each element in the current set of matched elements, up to but not including the element matched by the selector, DOM node, or jQuery object.
  1155. * @param {Selector|Element|$Kit.Suger Instance Object}
  1156. * @param {Element} [context]
  1157. * @return {Object} new $Kit.Suger Instance
  1158. */
  1159. parentsUntil : function(selector, context) {
  1160. var me = this;
  1161. context = context || document;
  1162. var me = this, element = me.nodes[0];
  1163. var find = $kit.selector.matches(selector, [element]);
  1164. var parentNode = me.nodes[0];
  1165. var re = [];
  1166. while(parentNode != context) {
  1167. parentNode = parentNode.parentNode;
  1168. if($kit.isStr(selector) && $kit.selector.matchesSelector(parentNode, selector)) {
  1169. break;
  1170. } else if($kit.isNode(selector) && parentNode == selector) {
  1171. break;
  1172. } else if(me.isSuger(selector) && parentNode == selector.nodes[0]) {
  1173. break;
  1174. } else {
  1175. re.push(me._new(parentNode));
  1176. }
  1177. }
  1178. if(re.length) {
  1179. return me._new(re);
  1180. }
  1181. return null;
  1182. },
  1183. /**
  1184. * 下一个元素
  1185. * Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.
  1186. * @see <a href="http://api.jquery.com/next/">详细看jQuery的解释,和他一样</a>
  1187. * @param {Selector} [selector]
  1188. * @return {Object} new $Kit.Suger Instance
  1189. */
  1190. next : function(selector) {
  1191. var re = [], me = this;
  1192. var nextElementSibling = me.nodes[0];
  1193. while(nextElementSibling.nextSibling) {
  1194. nextElementSibling = nextElementSibling.nextSibling;
  1195. if(nextElementSibling.nodeType == $kit.CONSTANTS.NODETYPE_ELEMENT) {
  1196. if($kit.isStr(selector)) {
  1197. if($kit.selector.matchesSelector(nextElementSibling, selector)) {
  1198. re.push(nextElementSibling);
  1199. break;
  1200. }
  1201. } else {
  1202. re.push(nextElementSibling);
  1203. break;
  1204. }
  1205. }
  1206. }
  1207. return me._new(re);
  1208. },
  1209. /**
  1210. * 后面所有元素
  1211. * Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.
  1212. * @see <a href="http://api.jquery.com/nextAll/">详细看jQuery的解释,和他一样</a>
  1213. * @param {Selector} [selector]
  1214. * @return {Object} new $Kit.Suger Instance
  1215. */
  1216. nextAll : function(selector) {
  1217. var re = [], me = this;
  1218. var nextElementSibling = me.nodes[0];
  1219. while(nextElementSibling.nextSibling) {
  1220. nextElementSibling = nextElementSibling.nextSibling;
  1221. if(nextElementSibling.nodeType == $kit.CONSTANTS.NODETYPE_ELEMENT) {
  1222. if($kit.isStr(selector)) {
  1223. if($kit.selector.matchesSelector(nextElementSibling, selector)) {
  1224. re.push(nextElementSibling);
  1225. }
  1226. } else {
  1227. re.push(nextElementSibling);
  1228. }
  1229. }
  1230. }
  1231. return me._new(re);
  1232. },
  1233. /**
  1234. * 同级后面所有,不包含selector
  1235. * Get all following siblings of each element up to but not including the element matched by the selector, DOM node, or jQuery object passed.
  1236. * @see <a href="http://api.jquery.com/nextUntil/">详细看jQuery的解释,和他一样</a>
  1237. * @param {Selector ...|Element ...|$Kit.Suger Instance Object ...}
  1238. * @return {Object} new $Kit.Suger Instance
  1239. */
  1240. nextUntil : function() {
  1241. var me = this, re = [];
  1242. var nextElementSibling = me.nodes[0];
  1243. while(nextElementSibling.nextSibling) {
  1244. nextElementSibling = nextElementSibling.nextSibling;
  1245. if(nextElementSibling.nodeType == $kit.CONSTANTS.NODETYPE_ELEMENT) {
  1246. var flag = true;
  1247. for(var i = 0; i < arguments.length; i++) {
  1248. var selector = arguments[i];
  1249. if($kit.isStr(selector) && $kit.selector.matchesSelector(nextElementSibling, selector)) {
  1250. flag = false;
  1251. break;
  1252. } else if($kit.isNode(selector) && nextElementSibling == selector) {
  1253. flag = false;
  1254. break;
  1255. }
  1256. }
  1257. if(flag) {
  1258. re.push(nextElementSibling);
  1259. }
  1260. }
  1261. }
  1262. return me._new(re);
  1263. },
  1264. /**
  1265. * 前面
  1266. * Get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.
  1267. * @see <a href="http://api.jquery.com/prev/">详细看jQuery的解释,和他一样</a>
  1268. * @param {Selector} [selector]
  1269. * @return {Object} new $Kit.Suger Instance
  1270. */
  1271. prev : function(selector) {
  1272. var me = this, re = [];
  1273. var previousElementSibling = me.nodes[0];
  1274. while(previousElementSibling.previousSibling) {
  1275. previousElementSibling = previousElementSibling.previousSibling;
  1276. if(previousElementSibling.nodeType == $kit.CONSTANTS.NODETYPE_ELEMENT) {
  1277. if($kit.isStr(selector)) {
  1278. if($kit.selector.matchesSelector(previousElementSibling, selector)) {
  1279. re.push(previousElementSibling);
  1280. break;
  1281. }
  1282. } else {
  1283. re.push(previousElementSibling);
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. return me._new(re);
  1289. },
  1290. /**
  1291. * 之前所有的
  1292. * Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.
  1293. * @see <a href="http://api.jquery.com/prevAll/">详细看jQuery的解释,和他一样</a>
  1294. * @param {Selector} [selector]
  1295. * @return {Object} new $Kit.Suger Instance
  1296. */
  1297. prevAll : function(selector) {
  1298. var me = this, re = [];
  1299. var previousElementSibling = me.nodes[0];
  1300. while(previousElementSibling.previousSibling) {
  1301. previousElementSibling = previousElementSibling.previousSibling;
  1302. if(previousElementSibling.nodeType == $kit.CONSTANTS.NODETYPE_ELEMENT) {
  1303. if($kit.isStr(selector)) {
  1304. if($kit.selector.matchesSelector(previousElementSibling, selector)) {
  1305. re.push(previousElementSibling);
  1306. }
  1307. } else {
  1308. re.push(previousElementSibling);
  1309. }
  1310. }
  1311. }
  1312. return me._new(re);
  1313. },
  1314. /**
  1315. * 之前所有的,直到
  1316. * Get all preceding siblings of each element up to but not including the element matched by the selector, DOM node, or jQuery object.
  1317. * @see <a href="http://api.jquery.com/prevUntil/">详细看jQuery的解释,和他一样</a>
  1318. * @param {Selector ...|Element ...|$Kit.Suger Instance Object ...}
  1319. * @return {Object} new $Kit.Suger Instance
  1320. */
  1321. prevUntil : function(selector) {
  1322. var me = this, re = [];
  1323. var previousElementSibling = me.nodes[0];
  1324. while(previousElementSibling.previousSibling) {
  1325. previousElementSibling = previousElementSibling.previousSibling;
  1326. if(previousElementSibling.nodeType == $kit.CONSTANTS.NODETYPE_ELEMENT) {
  1327. var flag = true;
  1328. for(var i = 0; i < arguments.length; i++) {
  1329. var selector = arguments[i];
  1330. if($kit.isStr(selector) && $kit.selector.matchesSelector(previousElementSibling, selector)) {
  1331. flag = false;
  1332. break;
  1333. } else if($kit.isNode(selector) && previousElementSibling == selector) {
  1334. flag = false;
  1335. break;
  1336. }
  1337. }
  1338. if(flag) {
  1339. re.push(previousElementSibling);
  1340. }
  1341. }
  1342. }
  1343. return me._new(re);
  1344. },
  1345. /**
  1346. * 删除
  1347. * Remove the set of matched elements from the DOM.
  1348. * @return {Object} current $Kit.Suger Instance
  1349. */
  1350. remove : function() {
  1351. var me = this;
  1352. $kit.each(me.nodes, function(o) {
  1353. $kit.rmEl(o);
  1354. });
  1355. return me;
  1356. },
  1357. /**
  1358. * 替换
  1359. * Replace each target element with the set of matched elements.
  1360. * @see <a href="http://api.jquery.com/replaceAll/">详细看jQuery的解释,和他一样</a>
  1361. * @param {Selector|Element|$Kit.Suger Instance Object}
  1362. * @param {Element} [context]
  1363. * @return {Object} current $Kit.Suger Instance
  1364. */
  1365. replaceAll : function(selector, context) {
  1366. var me = this;
  1367. var target;
  1368. if($kit.isNode(selector)) {
  1369. target = [selector];
  1370. } else if($kit.isStr(selector)) {
  1371. target = $kit.$el(selector, context);
  1372. } else if(me.isSuger(selector)) {
  1373. target = selector.nodes;
  1374. }
  1375. if(target) {
  1376. $kit.each(target, function(o) {
  1377. $kit.rpEl(o, me.nodes[0]);
  1378. });
  1379. }
  1380. return me;
  1381. },
  1382. /**
  1383. * Replace each element in the set of matched elements with the provided new content.\
  1384. * @see <a href="http://api.jquery.com/replaceWith/">详细看jQuery的解释,和他一样</a>
  1385. * @param {Selector|Element|$Kit.Suger Instance Object|Function}
  1386. * @param {Element} [context]
  1387. * @return {Object} current $Kit.Suger Instance
  1388. */
  1389. replaceWith : function(selector, context) {
  1390. var me = this;
  1391. var target;
  1392. if($kit.isNode(selector)) {
  1393. target = selector;
  1394. } else if(me.isSuger(selector)) {
  1395. target = selector.nodes[0];
  1396. } else if($kit.isStr(selector)) {
  1397. target = $kit.$el(selector, context)[0];
  1398. } else if($kit.isFn(selector)) {
  1399. target = selector.call(me.nodes[0]);
  1400. }
  1401. if(target) {
  1402. $kit.rpEl(me.nodes[0], target);
  1403. }
  1404. return me;
  1405. },
  1406. /**
  1407. * form元素序列化
  1408. */
  1409. serialize : function() {
  1410. var re = [], me = this;
  1411. $kit.each(me.nodes, function(o) {
  1412. re.push($kit.dom.serialize(o));
  1413. });
  1414. return re.join('&');
  1415. },
  1416. /**
  1417. * 找到所有同级其他元素
  1418. * @see <a href="http://api.jquery.com/siblings/">详细看jQuery的解释,和他一样</a>
  1419. * @param {Selector} [selector]
  1420. * @return {[Element]}
  1421. */
  1422. siblings : function(selector) {
  1423. var me = this;
  1424. var siblings = me.nodes[0].parent.childNodes, re = [];
  1425. $kit.each(siblings, function(o) {
  1426. if(o != me.nodes[0]) {
  1427. if(selector == null) {
  1428. re.push(o);
  1429. } else if($kit.selector.matchesSelector(o, selector)) {
  1430. re.push(o);
  1431. }
  1432. }
  1433. });
  1434. },
  1435. /**
  1436. * 返回nodes长度
  1437. * @return {Number}
  1438. */
  1439. size : function() {
  1440. return this.nodes.length;
  1441. },
  1442. /**
  1443. * Reduce the set of matched elements to a subset specified by a range of indices.
  1444. * @param {Number}
  1445. * @param {Number} [end]
  1446. * @return {Object} new $Kit.Suger Instance
  1447. */
  1448. slice : function(begin, end) {
  1449. if(end != null) {
  1450. return this._new(this.nodes.slice(begin, end));
  1451. } else if(begin != null) {
  1452. return this._new(this.nodes.slice(begin));
  1453. }
  1454. return this;
  1455. },
  1456. /**
  1457. * 获得value
  1458. * Get the current value of the first element in the set of matched elements.
  1459. * @return {String}
  1460. */
  1461. val : function() {
  1462. return $kit.val(me.nodes);
  1463. },
  1464. /**
  1465. * 包围
  1466. * Wrap an HTML structure around each element in the set of matched elements.
  1467. * @param {HTML|Element|$Kit.Suger Instance}
  1468. * @return {Object} new $Kit.Suger Instance
  1469. */
  1470. wrap : function(node) {
  1471. var me = this;
  1472. if($kit.isNode(node)) {
  1473. $kit.dom.wrap(me.nodes[0], node);
  1474. return me._new(node);
  1475. } else if(me.isSuger(node)) {
  1476. $kit.dom.wrap(me.nodes[0], node.nodes[0]);
  1477. return node;
  1478. } else if($kit.isStr(node)) {
  1479. node = $kit.newHTML(node).childNodes[0];
  1480. $kit.dom.wrap(me.nodes[0], node);
  1481. return me._new(node);
  1482. }
  1483. return null;
  1484. },
  1485. /**
  1486. * Wrap an HTML structure around all elements in the set of matched elements.
  1487. * @param {HTML|Element|$Kit.Suger Instance}
  1488. * @return {Object} new $Kit.Suger Instance
  1489. */
  1490. wrapAll : function(node) {
  1491. var me = this;
  1492. if($kit.isNode(node)) {
  1493. $kit.dom.wrap(me.nodes[0], node);
  1494. for(var i = 1; i < me.nodes.length; i++) {
  1495. node.appendChild(me.nodes[i]);
  1496. }
  1497. return me._new(node);
  1498. } else if(me.isSuger(node)) {
  1499. $kit.dom.wrap(me.nodes[0], node.nodes[0]);
  1500. for(var i = 1; i < me.nodes.length; i++) {
  1501. node.appendChild(me.nodes[i]);
  1502. }
  1503. return node;
  1504. } else if($kit.isStr(node)) {
  1505. node = $kit.newHTML(node).childNodes[0];
  1506. $kit.dom.wrap(me.nodes[0], node);
  1507. for(var i = 1; i < me.nodes.length; i++) {
  1508. node.appendChild(me.nodes[i]);
  1509. }
  1510. return me._new(node);
  1511. }
  1512. return null;
  1513. },
  1514. /**
  1515. * Wrap an HTML structure around the content of each element in the set of matched elements.
  1516. * @param {HTML|Element|$Kit.Suger Instance|Function}
  1517. * @return {Object} new $Kit.Suger Instance
  1518. */
  1519. wrapInner : function(node) {
  1520. var me = this;
  1521. if($kit.isNode(node)) {
  1522. me.nodes[0].appendChild(node);
  1523. var innerHTML = me.nodes[0].innerHTML;
  1524. me.nodes[0].innerHTML = '';
  1525. node.innerHTML = innerHTML;
  1526. return me._new(node);
  1527. } else if(me.isSuger(node)) {
  1528. me.nodes[0].appendChild(node.nodes[0]);
  1529. var innerHTML = me.nodes[0].innerHTML;
  1530. me.nodes[0].innerHTML = '';
  1531. node.nodes[0].innerHTML = innerHTML;
  1532. return node;
  1533. } else if($kit.isStr(node)) {
  1534. node = $kit.newHTML(node).childNodes[0];
  1535. me.nodes[0].appendChild(node);
  1536. var innerHTML = me.nodes[0].innerHTML;
  1537. me.nodes[0].innerHTML = '';
  1538. node.innerHTML = innerHTML;
  1539. return me._new(node);
  1540. } else if($kit.isFn(node)) {
  1541. node = node.call(me.nodes[0]);
  1542. return me.wrapInner(node);
  1543. }
  1544. return null;
  1545. },
  1546. /**
  1547. * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
  1548. * @return {Object} new $Kit.Suger Instance
  1549. */
  1550. unwrap : function() {
  1551. var me = this;
  1552. $kit.each(me.nodes, function(o) {
  1553. $kit.rpEl(o.parentNode, o);
  1554. });
  1555. return me;
  1556. },
  1557. /**
  1558. * 是否是kitSuger对象
  1559. * @param {Object}
  1560. * @return {Boolean}
  1561. * @private
  1562. */
  1563. isSuger : function(o) {
  1564. return o != null && $kit.isObj(o) && o.name == 'kitSuger';
  1565. },
  1566. /**
  1567. * 生成一个新的jquery队列
  1568. * @param {Selector|Element|[Element]|$Kit.Suger Instance}
  1569. * @return {Object} new $Kit.Suger Instance
  1570. */
  1571. pushStack : function(selector) {
  1572. var me = this;
  1573. return me._new(selector);
  1574. },
  1575. /**
  1576. * Get the children of each element in the set of matched elements, optionally filtered by a selector.
  1577. * @see <a href="http://api.jquery.com/children/">详细看jQuery的解释,和他一样</a>
  1578. * @param {Selector} [selector]
  1579. * @return {Object} new $Kit.Suger Instance
  1580. */
  1581. children : function(selector) {
  1582. var me = this;
  1583. var re = [];
  1584. $kit.each(me.nodes, function(o) {
  1585. if(selector == null) {
  1586. $kit.array.ad(re, o.childNodes, {
  1587. ifExisted : true
  1588. });
  1589. } else {
  1590. $kit.each(o.childNodes, function(o1) {
  1591. if($kit.selector.matchesSelector(selector, o1)) {
  1592. re.push(o1);
  1593. }
  1594. });
  1595. }
  1596. });
  1597. return me._new(re);
  1598. },
  1599. /**
  1600. * 返回DOM element
  1601. * @param {Number} index
  1602. * @return {[Element]|Element}
  1603. */
  1604. get : function(index) {
  1605. if(index == null) {
  1606. return this.nodes;
  1607. } else {
  1608. if(index > 0) {
  1609. return this.nodes[index];
  1610. } else {
  1611. return this.nodes[this.nodes.length - index];
  1612. }
  1613. }
  1614. }
  1615. });
  1616. if($kit.$) {
  1617. $kit._$ = $kit.$;
  1618. }
  1619. /**
  1620. * $kit.$方法
  1621. * @name $kit.$
  1622. * @global
  1623. * @function
  1624. * @param {Selector|Function|Element|[Element]} arg
  1625. * arg为function时候,相当于dom ready,会将$kit.$方法传递进去为参数1,
  1626. * 例如$kit.$(function($){$.xxxx})这里的function里面的$就等同于$kit.$,相当于别名的功能
  1627. * arg为其他类型的时候,自动生成一个$Kit.Suger实例,相当于jQuyer.$
  1628. * @param {Element} [context]
  1629. * @return {Null|$Kit.Suger Instance Object}
  1630. */
  1631. $k = $kit.$ = function() {
  1632. if(arguments[0] == null || arguments[0] == '') {
  1633. return;
  1634. }
  1635. if($kit.isFn(arguments[0])) {
  1636. var fn = arguments[0];
  1637. $kit._$(function() {
  1638. fn($kit.$);
  1639. });
  1640. } else {
  1641. return new $Kit.Suger(arguments[0], arguments[1]);
  1642. }
  1643. }
  1644. /**
  1645. * @module suger
  1646. * @return global.html#$kit.$el
  1647. */
  1648. if(window['define']) {
  1649. define('suger', ['math', 'anim', 'array', 'dom', 'io', 'json', 'selector', 'event'], function() {
  1650. return $kit.$;
  1651. });
  1652. }