【问题标题】:JQUERY error : null or not an object. Any Jquery Ninjas that can help?JQUERY 错误:null 或不是对象。任何可以提供帮助的 Jquery Ninjas?
【发布时间】:2011-03-27 01:49:42
【问题描述】:

那里有 JQUERY NINJA 吗?

在 IE 中出现错误?

'tip' is null or not an object

这是小脚本:

  $(document).ready(function() {
       //Tooltips
        var tip;
        $(".tip_trigger").hover(function(){

            //Caching the tooltip and removing it from container; then appending it to the body
            tip = $(this).find('.tip').remove();
            $('body').append(tip);

            tip.show(); //Show tooltip


        }, function() {

            tip.hide().remove(); //Hide and remove tooltip appended to the body
            $(this).append(tip); //Return the tooltip to its original position

        }).mousemove(function(e) {
        //console.log(e.pageX)
              var mousex = e.pageX + 20; //Get X coodrinates
              var mousey = e.pageY + 20; //Get Y coordinates
              var tipWidth = tip.width(); //Find width of tooltip
              var tipHeight = tip.height(); //Find height of tooltip

             //Distance of element from the right edge of viewport
              var tipVisX = $(window).width() - (mousex + tipWidth);
              var tipVisY = $(window).height() - (mousey + tipHeight);

            if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
                mousex = e.pageX - tipWidth - 20;
                $(this).find('.tip').css({  top: mousey, left: mousex });
            } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
                mousey = e.pageY - tipHeight - 20;
                tip.css({  top: mousey, left: mousex });
            } else {
                tip.css({  top: mousey, left: mousex });
            }
        });

    });

更新代码:(似乎无法将您更新的代码集成到此)

$(document).ready(function() {
   //Tooltips
    var tip = null;

    $(".tip_trigger").hover(function(){

        //Caching the tooltip and removing it from container; then appending it to the body
        tip = $(this).find('.tip').remove();
        $('body').append(tip);

        tip.show(); //Show tooltip


    }, function() {

        tip.hide().remove(); //Hide and remove tooltip appended to the body
        $(this).append(tip); //Return the tooltip to its original position

    }).mousemove(function(e) {
    //console.log(e.pageX)
          if ( tip == null ) return;


          var mousex = e.pageX + 20; //Get X coodrinates
          var mousey = e.pageY + 20; //Get Y coordinates
          var tipWidth = tip.width(); //Find width of tooltip
          var tipHeight = tip.height(); //Find height of tooltip

         //Distance of element from the right edge of viewport
          var tipVisX = $(window).width() - (mousex + tipWidth);
          var tipVisY = $(window).height() - (mousey + tipHeight);

        if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
            mousex = e.pageX - tipWidth - 20;
            $(this).find('.tip').css({  top: mousey, left: mousex });
        } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
            mousey = e.pageY - tipHeight - 20;
            tip.css({  top: mousey, left: mousex });
        } else {
            tip.css({  top: mousey, left: mousex });
        }
    });

});

【问题讨论】:

  • 这是在哪一行报告的?
  • 能否贴出HTML代码
  • @eberswine 请注意,您的 mousemove 回调中有 $(this).find('.tip') 而不是 tip。更正一下。
  • 是的,从上面更正。这就是我得到的错误行。
  • Muchas gracias,tenía varios problemas con el jquery,esta jsfiddle.net/vNBNz/4 fue la solución definitiva a mi problema。非常感谢。测试:redsaludcondorcanqui.com/establecimientos-de-salud 查看地图。

标签: jquery jquery-tooltip


【解决方案1】:
$(function() {

    $('.tip_trigger').each(function() {        
        var tip = $(this).find('.tip');

        $(this).hover(
            function() { tip.appendTo('body'); },
            function() { tip.appendTo(this); }
        ).mousemove(function(e) {
            var x = e.pageX + 20,
                y = e.pageY + 20,
                w = tip.width(),
                h = tip.height(),
                dx = $(window).width() - (x + w),
                dy = $(window).height() - (y + h);

            if ( dx < 20 ) x = e.pageX - w - 20;
            if ( dy < 20 ) y = e.pageY - h - 20;

            tip.css({ left: x, top: y });
        });         
    });

});

现场演示: http://jsfiddle.net/vNBNz/4/

如您所见,上述代码有效。在现场演示中,请注意以下 CSS 规则:

.tip_trigger .tip { display:none; }

上述规则将隐藏所有.tip 元素,但前提是它们位于.tip_trigger 元素内。这意味着,只要将 .tip 元素附加到 BODY,它就会显示出来,因为“隐藏规则”仅适用于 .tip_trigger 内的 .tip 元素。

【讨论】:

  • @eberswine 页面上有多少个工具提示(.tip 元素)?如果您有多个,那么您可能希望在悬停处理程序中设置 tip = null; 以清除下一个悬停/鼠标移动操作的 tip 变量。
  • @eberswine 我已经更新了我的代码。 ...我必须承认我对这个解决方案感到不舒服。在您有多个工具提示的情况下,使用单个局部变量来存储工具提示似乎不是最佳解决方案。我会考虑替代解决方案。
  • @eberswine 我已经用替代解决方案更新了我的答案。我更喜欢这个新的解决方案。
  • 嗨 Sime,这是我正在使用的代码,我正在尝试实现您的新方法,但没有运气:-(
  • @eberswine 如果您可以发布 HTML 源代码(至少是包含 .tip_trigger 元素的相关部分),或者即使您可以在线发布页面的实时演示,这将有所帮助。跨度>
【解决方案2】:

这是因为在 IE 中 move 可能发生在 hover 之前。试试out this example。我没有在 Chrome 中重现此功能。而我的 IE 测试是 IE9 beta。

所以我的猜测是它在这条线上失败了:

var tipWidth = tip.width();

mousemove 回调因为var tip; 仍然是undefined

【讨论】:

  • +1 这似乎是错误的原因。但是该怎么办呢?如何在 IE 中解决这个问题?
  • 是的,同样...仍然不是解决方案
  • 我如何在“编辑这个小提琴”中测试它没有做任何事情。我究竟做错了什么?谢谢
  • @eberswine 只需将鼠标光标悬停在“结果”框(右下角)中的文本即可。然后检查控制台(IE9 中的 F12)。
  • @Šime Vidas - 抱歉,伙计们正在玩一些 SC2 哈哈。很高兴您找到了解决方案。我认为问题的原因就足够了,因为从那里真的很简单:)
猜你喜欢
  • 2020-12-01
  • 2011-05-19
  • 2010-11-21
  • 2015-09-14
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
相关资源
最近更新 更多