DOM对象只需要在Internet Explorer中单击两次

DOM object only requires two clicks in Internet Explorer

本文关键字:单击 两次 Explorer 对象 Internet DOM      更新时间:2023-09-26

我真的很难处理下面的代码。我对将DOM与Javascript结合使用还很陌生,而且这个脚本在FireFox、Chrome和Safari中运行得非常完美。在Internet Explorer中,它需要单击两次。如果你访问FireFox中的链接,然后访问Internet Explorer中的同一链接,你会看到如果你在FireFox上单击一个形状,它会立即显示颜色选项。如果你在Internet Explorer中这样做,它不会显示颜色选项,直到你单击该形状两次,或者单击一个图形,然后单击另一个图形。IE、DOM、Javascript Ninja能告诉我导致在IE中需要双击的脚本出了什么问题吗?

<?php 
$swatches = $this->get_option_swatches();
?>
<script type="text/javascript">
    document.observe('dom:loaded', function() {
        try {
            var swatches = <?php echo Mage::helper('core')->jsonEncode($swatches); ?>;
            function find_swatch(key, value) {
                for (var i in swatches) {
                    if (swatches[i].key == key && swatches[i].value == value)
                        return swatches[i];
                }
                return null;
            }
            function has_swatch_key(key) {
                for (var i in swatches) {
                    if (swatches[i].key == key)
                        return true;
                }
                return false;
            }
            function create_swatches(label, select) {
                // create swatches div, and append below the <select>
                var sw = new Element('div', {'class': 'swatches-container'});
                select.up().appendChild(sw);
                // store these element to use later for recreate swatches
                select.swatchLabel = label;
                select.swatchElement = sw;
                // hide select
                select.setStyle({position: 'absolute', top: '-9999px'});
                $A(select.options).each(function(opt, i) {
                    if (opt.getAttribute('value')) {
                        var elm;
                        var key = trim(opt.innerHTML);
                        // remove price
                        if (opt.getAttribute('price')) key = trim(key.replace(/'+([^+]+)$/, ''));
                        var item = find_swatch(label, key);
                        if (item)
                            elm = new Element('img', {
                                src: '<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); ?>swatches/'+item.img, 
                                alt: opt.innerHTML, 
                                title: opt.innerHTML, 
                                'class': 'swatch-img'});
                        else {
                            console.debug(label, key, swatches);
                            elm = new Element('a', {'class': 'swatch-span'});
                            elm.update(opt.innerHTML);
                        }
                        elm.observe('click', function(event) {
                            select.selectedIndex = i;
                            fireEvent(select, 'change');
                            var cur = sw.down('.current');
                            if (cur) cur.removeClassName('current');
                            elm.addClassName('current');
                        });
                        sw.appendChild(elm);
                    }
                });
            }
            // Hide Second Option's Label
            function hideStuff(id) {
                if (document.getElementById(id)) {
                    document.getElementById(id).style.display = 'none';
                }
            }
            hideStuff("last-option-label");
            function showStuff(id) {
                if (document.getElementById(id)) {
                    document.getElementById(id).style.display = '';
                }
            }
            function recreate_swatches_recursive(select) {
                // remove the old swatches
                if (select.swatchElement) {
                    select.up().removeChild(select.swatchElement);
                    select.swatchElement = null;
                }
                // create again
                if (!select.disabled)
                    showStuff("last-option-label");
                    create_swatches(select.swatchLabel, select);
                // recursively recreate swatches for the next select
                if (select.nextSetting)
                    recreate_swatches_recursive(select.nextSetting);
            }
            function fireEvent(element,event){
                if (document.createEventObject){
                    // dispatch for IE
                    var evt = document.createEventObject();
                    return element.fireEvent('on'+event, evt);      
                }
                else{
                    // dispatch for firefox + others
                    var evt = document.createEvent("HTMLEvents");
                    evt.initEvent(event, true, true ); // event type,bubbling,cancelable
                    return !element.dispatchEvent(evt);
                }
            }
            function trim(str) {
                return str.replace(/^'s's*/, '').replace(/'s's*$/, '');
            }

            $$('#product-options-wrapper dt').each(function(dt) {
                // get custom option's label
                var label = '';
                $A(dt.down('label').childNodes).each(function(node) {
                    if (node.nodeType == 3) label += node.nodeValue;
                });
                label = trim(label);
                var dd = dt.next();
                var select = dd.down('select');
                if (select && has_swatch_key(label)) {
                    create_swatches(label, select);
                    // if configurable products, recreate swatches of the next select when the current select change
                    if (select.hasClassName('super-attribute-select')) {
                        select.observe('change', function() {
                            recreate_swatches_recursive(select.nextSetting);
                        });
                    }
                }
            });
        }
        catch(e) {
            alert("Color Swatches javascript error. Please report this error to support@galathemes.com. Error:" + e.message);
        }
    });
</script>

console.debug已被弃用。在"function create_swatches(label,select)"中写入"console.debug(label、key、swatches);"将其更改为"console.log(label、key、samples);",您可以一起删除这行代码。。。。。。。导致的错误