开发者

Javascript Tooltip Not Working in IE

Hello guys on my blog I use this script:

jQuery(document).ready(function () {
    jQuery(".postbox").hover(function () {
        tipinfo = $(this).find('.tipinfo');
        var t = jQuery(this).position() + jQuery(this).width();
        var leftto = t.left + jQuery(this).width() - 30;
        tipinfo.css({ 
            top: jQuery(this).position().top + jQuery(this).height() - 2, 
            left: leftto 
        });
        tipinfo.show(); //vedi tooltip
    }, function () {
        tipinfo.hide(); //nascondi tooltip
    })
});

The tooltip works perfectly with all browsers except IE. I know that the state Hover the browser from Microsoft is not working, so I ask for your help. There is a hack to remedy the problem?

I would be very grateful for your help

The CSS:

.tipinfo {
    border-radius: 3px 3px 3px 3px;
    color: #FFFFFF;
    display: none;
    position: absolute;
    width: 350px;
    z-index: 1000;
}

.bgbull {
    background: url(images/bgbulle.png) no-repeat scroll 0 0 transparent;
    height: 29px;
    line-height: 35px;
    padding-left: 50px;
    width: 308px;
}

.infob {
    background: none repeat scroll 0 0 #151515;
    border-bottom: 1px solid #151515;
    border-left: 1px solid #151515;
    border-right: 1px solid #151515;
    line-height:1.2em;
    padding: 5px
    ;width: 296px;
}

.bgbullbottom {
    background: url(images/bgbullebottom.png) no-repeat scroll 0 0 transparent;
    height: 29px;
    line-height: 35px;
    padding-left: 50开发者_运维百科px;
    width: 308px;
}

HTML Code:

<div class="tipinfo" style="display:none;">
<div class="bgbull"></div>
<div class="infob">
<h4 style="color:#494949;font-family:arial,sans-serif;font-size:12px;font-weight:bold;"><?php    the_title_attribute(); ?></h4>
<br>
<span style="color:#bbb;"><?php the_excerpt(); ?></span>
<br>
<div style="margin-top:2px;border-top:1px dotted #323232;"></div><br>
<div style="color:#bbb;">
<span class="info">Genere: </span><br />
<span class="info">Durata: </span><br />
<span class="info">Non sai se ti piace? </span>
</div>
<div class="clear"></div>
</div>
<div class="bgbullbottom"></div>
</div>


This like looks wrong to me:

var t = jQuery(this).position() + jQuery(this).width();

Because jQuery(this).position() returns object, but jQuery(this).width() returns integer. Probably you should change it to :

var t = jQuery(this).position();
t.left += jQuery(this).width();


As far as I know you need to add the hover behaviour to IE<8. I don't know if jquery adds this functionality back, include the following line in your CSS:

body {/* ...*/behavior:url("/styles/csshover.htc");}

And the following in csshover.htc (copied with original credit, can find it on the listed page anymore)

<attach event="ondocumentready" handler="parseStylesheets" />
<script language="JScript">
/**
 *  Pseudos - V1.30.050121 - hover & active
 *  ---------------------------------------------
 *  Peterned - http://www.xs4all.nl/~peterned/
 *  (c) 2005 - Peter Nederlof
 *
 *  Credits  - Arnoud Berendsen 
 *           - Martin Reurings
 *           - Robert Hanson
 *
 *  howto: body { behavior:url("csshover.htc"); }
 *  ---------------------------------------------
 */

var currentSheet, doc = window.document, activators = {
    onhover:{on:'onmouseover', off:'onmouseout'},
    onactive:{on:'onmousedown', off:'onmouseup'}
}

function parseStylesheets() {
    var sheets = doc.styleSheets, l = sheets.length;
    for(var i=0; i<l; i++) 
        parseStylesheet(sheets[i]);
}
    function parseStylesheet(sheet) {
        if(sheet.imports) {
            try {
                var imports = sheet.imports, l = imports.length;
                for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);
            } catch(securityException){}
        }

        try {
            var rules = (currentSheet = sheet).rules, l = rules.length;
            for(var j=0; j<l; j++) parseCSSRule(rules[j]);
        } catch(securityException){}
    }

    function parseCSSRule(rule) {
        var select = rule.selectorText, style = rule.style.cssText;
        if(!(/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i).test(select) || !style) return;

        var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, 'on$1');
        var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);
        var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];
        var affected = select.replace(/:hover.*$/, '');
        var elements = getElementsBySelect(affected);

        currentSheet.addRule(newSelect, style);
        for(var i=0; i<elements.length; i++)
            new HoverElement(elements[i], className, activators[pseudo]);
    }

function HoverElement(node, className, events) {
    if(!node.hovers) node.hovers = {};
    if(node.hovers[className]) return;
    node.hovers[className] = true;
    node.attachEvent(events.on,
        function() { node.className += ' ' + className; });
    node.attachEvent(events.off,
        function() { node.className = 
            node.className.replace(new RegExp('\\s+'+className, 'g'),''); });
}

function getElementsBySelect(rule) {
    var parts, nodes = [doc];
    parts = rule.split(' ');
    for(var i=0; i<parts.length; i++) {
        nodes = getSelectedNodes(parts[i], nodes);
    }   return nodes;
}
    function getSelectedNodes(select, elements) {
        var result, node, nodes = [];
        var classname = (/\.([a-z0-9_-]+)/i).exec(select);
        var identify = (/\#([a-z0-9_-]+)/i).exec(select);
        var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');
        for(var i=0; i<elements.length; i++) {
            result = tagName? elements[i].all.tags(tagName):elements[i].all; 
            for(var j=0; j<result.length; j++) {
                node = result[j];
                if((identify && node.id != identify[1]) || (classname && !(new RegExp('\\b' +
                    classname[1] + '\\b').exec(node.className)))) continue;
                nodes[nodes.length] = node;
            }
        }   return nodes;
    }
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜