簡體   English   中英

重構代碼以在jQuery 1.3.2上工作

[英]Refactoring Code to Work on jQuery 1.3.2

我有一些代碼正在為客戶端編寫,它們目前正在使用Drupal 6和jQuery 1.3.2-我無法控制它們使用的jQuery,因此我嘗試根據以下假設編寫一些代碼:它不會很快改變。 我一直在重新編寫代碼,現在它可以工作到1.4,但是我碰到一堵磚牆,試圖使其在1.3上工作。

我正在使用FlatUI的單選按鈕-原來在這里( https://raw.github.com/designmodo/Flat-UI/master/js/flatui-radio.js)-我已經對其進行了重新設計,但是找不到阻止它觸發。

我設置了一個JSFiddle來演示我的問題。 除了年齡選擇部分之外,其他所有功能都可以正常工作,在每個年齡段的左側(13歲以上,14歲以上等)應該都有復選框。 正如我所說的,它可以追溯到1.4甚至在瀏覽jQuery文檔時,我找不到在1.3上不起作用的其他內容。

http://jsfiddle.net/dvw9F/

這是無效的代碼:

/* =============================================================
* flatui-radio.js v0.0.3
* ============================================================ */

(function ($) {

'use strict';

/* RADIO PUBLIC CLASS DEFINITION
* ============================== */

var Radio = function (element, options) {
    this.init(element, options);
};

Radio.prototype = {

    constructor: Radio,

    init: function (element, options) {

        var $el = this.$element = $(element);

        this.options = $.extend({}, $.fn.radio.defaults, options);
        $el.before(this.options.template);
        this.setState();

    },

    setState: function () {

        var $el = this.$element,
            $parent = $el.closest('.radio');

        if ($el.attr('disabled') === true) {
            $parent.addClass('disabled');
        }

        if ($el.attr('checked') === true) {
            $parent.addClass('checked');
        }

    },

    toggle: function () {

        var d = 'disabled',
            ch = 'checked',
            $el = this.$element,
            checked = false,
            $parent = $el.closest('.radio'),
            $parentWrap = $el.closest('form').length ? $el.closest('form') : $el.closest('body'),
            $elemGroup = $parentWrap.find(':radio[name="' + $el.attr('name') + '"]'),
            e = $.Event('toggle');

        if ($el.attr(ch) === true) {
            checked = true;
        }

        $elemGroup.not($el).each(function () {

            var $el = $(this),
                $parent = $(this).closest('.radio');

            if ($el.attr(d) !== true) {
                $parent.removeClass(ch) && $el.removeAttr(ch).trigger('change');
            }

        });

        if ($el.attr(d) !== true) {

            if (checked === false) {
                $parent.addClass(ch) && $el.attr(ch, true);
            }

            $el.trigger(e);

            if (true !== $el.attr(ch)) {
                $el.trigger('change');
            }

        }

    },

    setCheck: function (option) {

        var ch = 'checked',
            $el = this.$element,
            $parent = $el.closest('.radio'),
            checkAction = option === 'check' ? true : false,
            checked = false,
            $parentWrap = $el.closest('form').length ? $el.closest('form') : $el.closest('body'),
            $elemGroup = $parentWrap.find(':radio[name="' + $el.attr('name') + '"]'),
            e = $.Event(option);

        if ($el.attr(ch) === true) {
            checked = true;
        }

        $elemGroup.not($el).each(function () {

            var $el = $(this),
                $parent = $(this).closest('.radio');

            $parent.removeClass(ch) && $el.removeAttr(ch);

        });

        $parent[checkAction ? 'addClass' : 'removeClass'](ch) && checkAction ? $el.attr(ch, ch) : $el.removeAttr(ch);
        $el.trigger(e);

        if (checked !== $el.attr(ch)) {
            $el.trigger('change');
        }

    }

};

/* RADIO PLUGIN DEFINITION
* ======================== */

var old = $.fn.radio;

$.fn.radio = function (option) {

    return this.each(function () {

        var $this = $(this),
            data = $this.data('radio'),
            options = $.extend({}, $.fn.radio.defaults, $this.data(), typeof option === 'object' && option);

        if (!data) {
            $this.data('radio', (data = new Radio(this, options)));
        }

        if (option === 'toggle') {
            data.toggle();
        }

        if (option === 'check' || option === 'uncheck') {
            data.setCheck(option);
        } else if (option) {
            data.setState();
        }

    });

};

$.fn.radio.defaults = {
    template: '<span class="icons"><span class="first-icon fui-radio-unchecked"></span><span class="second-icon fui-radio-checked"></span></span>'
};

/* RADIO NO CONFLICT
* ================== */

$.fn.radio.noConflict = function () {

    $.fn.radio = old;
    return this;

};

/* RADIO DATA-API
* =============== */

$('.radio').live('click', '[data-toggle^=radio], .radio', function (e) {

    var $radio = $(e.target);

    if (e.target.tagName !== "A") {

        e && e.preventDefault() && e.stopPropagation();
        if (!$radio.hasClass('radio')) {
            $radio = $radio.closest('.radio');
        }

        $radio.find(':radio').radio('toggle');

    }

});

$(function () {

    $('[data-toggle="radio"]').each(function () {
        var $radio = $(this);
        $radio.radio();
    });

});

}(jQuery));

如果將現有代碼轉換為以前的版本,僅僅是因為某些插件需要在舊版本上運行。 我建議您不要這樣做。

您可以在同一頁面上使用不同的版本,因此無需更新現有代碼:

<!-- load jQuery version 1.9.0 -->
<script src="jquery-1.9.0.js" type="text/javascript"></script>
<script type="text/javascript">
var $jQuery1_9 = $.noConflict(true);// Here you must need to pass true
                                    //else it will only free $ variable
                                    //and using jQuery with blow libraries
                                    //cause conflict
</script>

//you must load second library later after initializing
//first instance of version and freeup jQuery keyword
//else jQuery keyword will
//cause conflict it you uplaoded both files.

<!-- load jQuery version 1.10.0 -->
<script src="jquery-1.10.0.js" type="text/javascript"></script>
<script type="text/javascript">
var $jQuery1_10 = $.noConflict(true);
</script>

//so now here $ and jQuery both cannot be used

//using $jQuery1_10 will call version 1.10.0 library code
$jQuery1_10( "div p" ).show();

//using $jQuery1_9 will call version 1.9.0 library code
$jQuery1_9( "div p" ).show();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM