javascript – iCheck不起作用(奇怪的行为)

前端之家收集整理的这篇文章主要介绍了javascript – iCheck不起作用(奇怪的行为)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用iCheck库:
https://github.com/fronteed/iCheck
http://fronteed.com/iCheck/

当我使用库初始化元素时,它会像文档描述https://github.com/fronteed/iCheck/#how-it-works那样变形但是不透明度为零.

你可以在这里看到它:http://jsfiddle.net/buRq7/8/

$( “EX-F”.)I检查().

有人有同样的问题吗?

谢谢

我的代码是:

  1. /*!
  2. * iCheck v1.0.1,http://git.io/arlzeA
  3. * =================================
  4. * Powerful jQuery and Zepto plugin for checkBoxes and radio buttons customization
  5. *
  6. * (c) 2013 Damir Sultanov,http://fronteed.com
  7. * MIT Licensed
  8. */
  9.  
  10. (function($) {
  11.  
  12. // Cached vars
  13. var _iCheck = 'iCheck',_iCheckHelper = _iCheck + '-helper',_checkBox = 'checkBox',_radio = 'radio',_checked = 'checked',_unchecked = 'un' + _checked,_disabled = 'disabled',_determinate = 'determinate',_indeterminate = 'in' + _determinate,_update = 'update',_type = 'type',_click = 'click',_touch = 'touchbegin.i touchend.i',_add = 'addClass',_remove = 'removeClass',_callback = 'trigger',_label = 'label',_cursor = 'cursor',_mobile = /ipad|iphone|ipod|android|blackberry|windows phone|opera mini|silk/i.test(navigator.userAgent);
  14.  
  15. // Plugin init
  16. $.fn[_iCheck] = function(options,fire) {
  17.  
  18. // Walker
  19. var handle = 'input[type="' + _checkBox + '"],input[type="' + _radio + '"]',stack = $(),walker = function(object) {
  20. object.each(function() {
  21. var self = $(this);
  22.  
  23. if (self.is(handle)) {
  24. stack = stack.add(self);
  25. } else {
  26. stack = stack.add(self.find(handle));
  27. };
  28. });
  29. };
  30.  
  31. // Check if we should operate with some method
  32. if (/^(check|uncheck|toggle|indeterminate|determinate|disable|enable|update|destroy)$/i.test(options)) {
  33.  
  34. // Normalize method's name
  35. options = options.toLowerCase();
  36.  
  37. // Find checkBoxes and radio buttons
  38. walker(this);
  39.  
  40. return stack.each(function() {
  41. var self = $(this);
  42.  
  43. if (options == 'destroy') {
  44. tidy(self,'ifDestroyed');
  45. } else {
  46. operate(self,true,options);
  47. };
  48.  
  49. // Fire method's callback
  50. if ($.isFunction(fire)) {
  51. fire();
  52. };
  53. });
  54.  
  55. // Customization
  56. } else if (typeof options == 'object' || !options) {
  57.  
  58. // Check if any options were passed
  59. var settings = $.extend({
  60. checkedClass: _checked,disabledClass: _disabled,indeterminateClass: _indeterminate,labelHover: true,aria: false
  61. },options),selector = settings.handle,hoverClass = settings.hoverClass || 'hover',focusClass = settings.focusClass || 'focus',activeClass = settings.activeClass || 'active',labelHover = !!settings.labelHover,labelHoverClass = settings.labelHoverClass || 'hover',// Setup clickable area
  62. area = ('' + settings.increaseArea).replace('%','') | 0;
  63.  
  64. // Selector limit
  65. if (selector == _checkBox || selector == _radio) {
  66. handle = 'input[type="' + selector + '"]';
  67. };
  68.  
  69. // Clickable area limit
  70. if (area < -50) {
  71. area = -50;
  72. };
  73.  
  74. // Walk around the selector
  75. walker(this);
  76.  
  77. return stack.each(function() {
  78. var self = $(this);
  79.  
  80. // If already customized
  81. tidy(self);
  82.  
  83. var node = this,id = node.id,// Layer styles
  84. offset = -area + '%',size = 100 + (area * 2) + '%',layer = {
  85. position: 'absolute',top: offset,left: offset,display: 'block',width: size,height: size,margin: 0,padding: 0,background: '#fff',border: 0,opacity: 0
  86. },// Choose how to hide input
  87. hide = _mobile ? {
  88. position: 'absolute',visibility: 'hidden'
  89. } : area ? layer : {
  90. position: 'absolute',// Get proper class
  91. className = node[_type] == _checkBox ? settings.checkBoxClass || 'i' + _checkBox : settings.radioClass || 'i' + _radio,// Find assigned labels
  92. label = $(_label + '[for="' + id + '"]').add(self.closest(_label)),// Check ARIA option
  93. aria = !!settings.aria,// Set ARIA placeholder
  94. ariaID = _iCheck + '-' + Math.random().toString(36).replace('0.',''),// Parent & helper
  95. parent = '<div class="' + className + '" ' + (aria ? 'role="' + node[_type] + '" ' : ''),helper;
  96.  
  97. // Set ARIA "labelledby"
  98. if (label.length && aria) {
  99. label.each(function() {
  100. parent += 'aria-labelledby="';
  101.  
  102. if (this.id) {
  103. parent += this.id;
  104. } else {
  105. this.id = ariaID;
  106. parent += ariaID;
  107. }
  108.  
  109. parent += '"';
  110. });
  111. };
  112.  
  113. // Wrap input
  114. parent = self.wrap(parent + '/>')[_callback]('ifCreated').parent().append(settings.insert);
  115.  
  116. // Layer addition
  117. helper = $('<ins class="' + _iCheckHelper + '"/>').css(layer).appendTo(parent);
  118.  
  119. // Finalize customization
  120. self.data(_iCheck,{o: settings,s: self.attr('style')}).css(hide);
  121. !!settings.inheritClass && parent[_add](node.className || '');
  122. !!settings.inheritID && id && parent.attr('id',_iCheck + '-' + id);
  123. parent.css('position') == 'static' && parent.css('position','relative');
  124. operate(self,_update);
  125.  
  126. // Label events
  127. if (label.length) {
  128. label.on(_click + '.i mouSEOver.i mouSEOut.i ' + _touch,function(event) {
  129. var type = event[_type],item = $(this);
  130.  
  131. // Do nothing if input is disabled
  132. if (!node[_disabled]) {
  133.  
  134. // Click
  135. if (type == _click) {
  136. if ($(event.target).is('a')) {
  137. return;
  138. }
  139. operate(self,false,true);
  140.  
  141. // Hover state
  142. } else if (labelHover) {
  143.  
  144. // mouSEOut|touchend
  145. if (/ut|nd/.test(type)) {
  146. parent[_remove](hoverClass);
  147. item[_remove](labelHoverClass);
  148. } else {
  149. parent[_add](hoverClass);
  150. item[_add](labelHoverClass);
  151. };
  152. };
  153.  
  154. if (_mobile) {
  155. event.stopPropagation();
  156. } else {
  157. return false;
  158. };
  159. };
  160. });
  161. };
  162.  
  163. // Input events
  164. self.on(_click + '.i focus.i blur.i keyup.i keydown.i keypress.i',function(event) {
  165. var type = event[_type],key = event.keyCode;
  166.  
  167. // Click
  168. if (type == _click) {
  169. return false;
  170.  
  171. // Keydown
  172. } else if (type == 'keydown' && key == 32) {
  173. if (!(node[_type] == _radio && node[_checked])) {
  174. if (node[_checked]) {
  175. off(self,_checked);
  176. } else {
  177. on(self,_checked);
  178. };
  179. };
  180.  
  181. return false;
  182.  
  183. // Keyup
  184. } else if (type == 'keyup' && node[_type] == _radio) {
  185. !node[_checked] && on(self,_checked);
  186.  
  187. // Focus/blur
  188. } else if (/us|ur/.test(type)) {
  189. parent[type == 'blur' ? _remove : _add](focusClass);
  190. };
  191. });
  192.  
  193. // Helper events
  194. helper.on(_click + ' mousedown mouseup mouSEOver mouSEOut ' + _touch,// mousedown|mouseup
  195. toggle = /wn|up/.test(type) ? activeClass : hoverClass;
  196.  
  197. // Do nothing if input is disabled
  198. if (!node[_disabled]) {
  199.  
  200. // Click
  201. if (type == _click) {
  202. operate(self,true);
  203.  
  204. // Active and hover states
  205. } else {
  206.  
  207. // State is on
  208. if (/wn|er|in/.test(type)) {
  209.  
  210. // mousedown|mouSEOver|touchbegin
  211. parent[_add](toggle);
  212.  
  213. // State is off
  214. } else {
  215. parent[_remove](toggle + ' ' + activeClass);
  216. };
  217.  
  218. // Label hover
  219. if (label.length && labelHover && toggle == hoverClass) {
  220.  
  221. // mouSEOut|touchend
  222. label[/ut|nd/.test(type) ? _remove : _add](labelHoverClass);
  223. };
  224. };
  225.  
  226. if (_mobile) {
  227. event.stopPropagation();
  228. } else {
  229. return false;
  230. };
  231. };
  232. });
  233. });
  234. } else {
  235. return this;
  236. };
  237. };
  238.  
  239. // Do something with inputs
  240. function operate(input,direct,method) {
  241. var node = input[0],state = /er/.test(method) ? _indeterminate : /bl/.test(method) ? _disabled : _checked,active = method == _update ? {
  242. checked: node[_checked],disabled: node[_disabled],indeterminate: input.attr(_indeterminate) == 'true' || input.attr(_determinate) == 'false'
  243. } : node[state];
  244.  
  245. // Check,disable or indeterminate
  246. if (/^(ch|di|in)/.test(method) && !active) {
  247. on(input,state);
  248.  
  249. // Uncheck,enable or determinate
  250. } else if (/^(un|en|de)/.test(method) && active) {
  251. off(input,state);
  252.  
  253. // Update
  254. } else if (method == _update) {
  255.  
  256. // Handle states
  257. for (var state in active) {
  258. if (active[state]) {
  259. on(input,state,true);
  260. } else {
  261. off(input,true);
  262. };
  263. };
  264.  
  265. } else if (!direct || method == 'toggle') {
  266.  
  267. // Helper or label was clicked
  268. if (!direct) {
  269. input[_callback]('ifClicked');
  270. };
  271.  
  272. // Toggle checked state
  273. if (active) {
  274. if (node[_type] !== _radio) {
  275. off(input,state);
  276. };
  277. } else {
  278. on(input,state);
  279. };
  280. };
  281. };
  282.  
  283. // Add checked,disabled or indeterminate state
  284. function on(input,keep) {
  285. var node = input[0],parent = input.parent(),checked = state == _checked,indeterminate = state == _indeterminate,disabled = state == _disabled,callback = indeterminate ? _determinate : checked ? _unchecked : 'enabled',regular = option(input,callback + capitalize(node[_type])),specific = option(input,state + capitalize(node[_type]));
  286.  
  287. // Prevent unnecessary actions
  288. if (node[state] !== true) {
  289.  
  290. // Toggle assigned radio buttons
  291. if (!keep && state == _checked && node[_type] == _radio && node.name) {
  292. var form = input.closest('form'),inputs = 'input[name="' + node.name + '"]';
  293.  
  294. inputs = form.length ? form.find(inputs) : $(inputs);
  295.  
  296. inputs.each(function() {
  297. if (this !== node && $(this).data(_iCheck)) {
  298. off($(this),state);
  299. };
  300. });
  301. };
  302.  
  303. // Indeterminate state
  304. if (indeterminate) {
  305.  
  306. // Add indeterminate state
  307. node[state] = true;
  308.  
  309. // Remove checked state
  310. if (node[_checked]) {
  311. off(input,_checked,'force');
  312. };
  313.  
  314. // Checked or disabled state
  315. } else {
  316.  
  317. // Add checked or disabled state
  318. if (!keep) {
  319. node[state] = true;
  320. };
  321.  
  322. // Remove indeterminate state
  323. if (checked && node[_indeterminate]) {
  324. off(input,_indeterminate,false);
  325. };
  326. };
  327.  
  328. // Trigger callbacks
  329. callbacks(input,checked,keep);
  330. };
  331.  
  332. // Add proper cursor
  333. if (node[_disabled] && !!option(input,_cursor,true)) {
  334. parent.find('.' + _iCheckHelper).css(_cursor,'default');
  335. };
  336.  
  337. // Add state class
  338. parent[_add](specific || option(input,state) || '');
  339.  
  340. // Set ARIA attribute
  341. disabled ? parent.attr('aria-disabled','true') : parent.attr('aria-checked',indeterminate ? 'mixed' : 'true');
  342.  
  343. // Remove regular state class
  344. parent[_remove](regular || option(input,callback) || '');
  345. };
  346.  
  347. // Remove checked,disabled or indeterminate state
  348. function off(input,state + capitalize(node[_type]));
  349.  
  350. // Prevent unnecessary actions
  351. if (node[state] !== false) {
  352.  
  353. // Toggle state
  354. if (indeterminate || !keep || keep == 'force') {
  355. node[state] = false;
  356. };
  357.  
  358. // Trigger callbacks
  359. callbacks(input,callback,keep);
  360. };
  361.  
  362. // Add proper cursor
  363. if (!node[_disabled] && !!option(input,'pointer');
  364. };
  365.  
  366. // Remove state class
  367. parent[_remove](specific || option(input,'false') : parent.attr('aria-checked','false');
  368.  
  369. // Add regular state class
  370. parent[_add](regular || option(input,callback) || '');
  371. };
  372.  
  373. // Remove all traces
  374. function tidy(input,callback) {
  375. if (input.data(_iCheck)) {
  376.  
  377. // Remove everything except input
  378. input.parent().html(input.attr('style',input.data(_iCheck).s || ''));
  379.  
  380. // Callback
  381. if (callback) {
  382. input[_callback](callback);
  383. };
  384.  
  385. // Unbind events
  386. input.off('.i').unwrap();
  387. $(_label + '[for="' + input[0].id + '"]').add(input.closest(_label)).off('.i');
  388. };
  389. };
  390.  
  391. // Get some option
  392. function option(input,regular) {
  393. if (input.data(_iCheck)) {
  394. return input.data(_iCheck).o[state + (regular ? '' : 'Class')];
  395. };
  396. };
  397.  
  398. // Capitalize some string
  399. function capitalize(string) {
  400. return string.charAt(0).toUpperCase() + string.slice(1);
  401. };
  402.  
  403. // Executable handlers
  404. function callbacks(input,keep) {
  405. if (!keep) {
  406. if (checked) {
  407. input[_callback]('ifToggled');
  408. };
  409.  
  410. input[_callback]('ifChanged')[_callback]('if' + capitalize(callback));
  411. };
  412. };
  413. })(window.jQuery || window.Zepto);
  414.  
  415. $(".ex-f").iCheck();

  1. <div class="my-checkBox">
  2. <input class="ex-f" id="Finish" name="Finish" type="checkBox" value="1"> doesn't work
  3. </div>

  1. .my-checkBox {
  2. border: 1px solid blue;
  3. height: 20px;
  4. }
  5. .ex-f {
  6. border: 1px solid red;
  7. }

解决方法

我发现问题出在CSS文件中,如果没有加载组件没有显示,一个工作示例:

http://jsfiddle.net/SUTMf/1/

$(文件).就绪(函数(){$( ‘输入’).I检查({checkBoxClass:’icheckBox_flat-blue’,radioClass:’iradio_square’,increaseArea:’20%’//可选});});

猜你在找的JavaScript相关文章