简体   繁体   English

检查像萤火虫一样的html元素

[英]inspecting html elements like firebug do

i am trying to make element inspector in jquery, i also almost done, but it works some times (not properly working). 我试图在jquery中使元素检查器,我也差不多完成了,但它有时会工作(不能正常工作)。 my code ---> 我的代码--->

here the fiddle ---> http://jsfiddle.net/ashokdamani/01ntuu6e/ 这里的小提琴---> http://jsfiddle.net/ashokdamani/01ntuu6e/

Html---> HTML --->

<div class="contentContainer">
        <input class="inputText inputText1" name="myName" type="text" style="margin-left: 50px;" />
        <input class="inputText " name="myData" type="text" />
        <div class="focusable" style="border: 1px solid green; width: 200px; height: 250px; margin: 50px;"></div>
    </div>

Jquery---> jQuery --->

    var ad_focused;
        var $ad_focusedele;
        var ad_mousetop;
        var ad_mouseleft;

$('body').mousemove(function (eve) {
                ad_mousetop = eve.pageY;
                ad_mouseleft = eve.pageX;

                if (ad_focused) {
                    if (isUnHovered($ad_focusedele)) {
                        $(".hoverDark").remove();
                        $(".hoverLight").remove();
                        $ad_focusedele = null;
                        ad_focused = false;
                    }
                }
            });

            $eles = $('div, input, form').not('body > div, body > form, body > iframe');

            $eles.hover(function () {
                if (ad_focused)
                    return;

                ad_focused = true;
                $ad_focusedele = $(this);

                var ad_wr = '<div class="hoverDark"></div><div class="hoverLight"></div>';
                $(this).parent().append(ad_wr);

                var mleft = !isNull($(this).css('margin-left')) ? parseInt(($(this).css('margin-left')).replace('px', '')) : 0;
                var mright = !isNull($(this).css('margin-right')) ? parseInt(($(this).css('margin-right')).replace('px', '')) : 0;
                var mtop = !isNull($(this).css('margin-top')) ? parseInt(($(this).css('margin-top')).replace('px', '')) : 0;
                var mbottom = !isNull($(this).css('margin-bottom')) ? parseInt(($(this).css('margin-bottom')).replace('px', '')) : 0;

                var elemOuterWidth = $(this).outerWidth(true) - (mleft == 0 ? 0 : mleft - 5) - (mright == 0 ? 0 : mright - 5);
                var elemOuterHeight = $(this).outerHeight(true) - (mtop == 0 ? 0 : mtop - 5) - (mbottom == 0 ? 0 : mbottom - 5);
                var elemPos = $(this).position();
                var elemPosTop = elemPos.top - 1;
                var elemPosLeft = elemPos.left - 1;

                $(".hoverDark").css({
                    "width": elemOuterWidth,
                    "height": elemOuterHeight,
                    "top": elemPosTop + (mtop == 0 ? 0 : mtop - 5),
                    "left": elemPosLeft + (mleft == 0 ? 0 : mleft - 5)
                });

                $(".hoverLight").css({
                    "width": elemOuterWidth,
                    "height": elemOuterHeight,
                    "top": elemPosTop + (mtop == 0 ? 0 : mtop - 5),
                    "left": elemPosLeft + (mleft == 0 ? 0 : mleft - 5)
                });
                $(".hoverDark").stop(true, true).show();
                $(".hoverLight").stop(true, true).show();
            });

function isUnHovered(obj) {
            var eletop = $(obj).offset().top;
            var eleleft = $(obj).offset().left;
            var eleright = $(obj).offset().left + getCssValue($(obj).css('width'));
            var elebottom = $(obj).offset().top + getCssValue($(obj).css('height'));

            return !(ad_mouseleft > eleleft && ad_mouseleft < eleright && ad_mousetop > eletop && ad_mousetop < elebottom);
        }

        function getCssValue(val) {
            return parseInt(val.replace('px', ''));
        }

        function isNull(obj) {
            return obj == null || obj == undefined;
        }

I am wrapping up the element when hover and unwrapping it when mouse not moving on it (ie unhover, but not exactly unhover ,because when it wrapped with some div its loops the hover event). 我将鼠标悬停时包装元素,而当鼠标不在其上移动时将其展开(即,悬停,但不完全悬停,因为当它被某些div包裹时,其循环了悬停事件)。

Instead of using hover event function it is always better to use jquery's mouseenter and mouseleave function for better response from html elements. 最好使用jquery的mouseentermouseleave函数来更好地响应html元素,而不是使用悬停事件函数。 This will solve your tricky problem...!! 这将解决您棘手的问题...!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用浏览器工具检查与使用 Javascript 获取尺寸时,HTML 元素的大小不同 - Size of HTML elements are different when inspecting with browser tools vs. getting size with Javascript 像jQuery实现萤火虫 - firebug like jquery implementation Firebug用于调试jQuery元素 - Firebug for debugging jQuery elements 如何查看iframe文档中的元素(如Firebug)? - How can I see the elements inside iframe documents like Firebug does? 跨域策略iFrame-检查元素? - Cross Domain Policy iFrame - inspecting for elements? Linux Compiz 对 HTML 元素的影响 - Linux Compiz like effects on HTML elements 如何在iframe中使用jQuery确定元素的祖先(例如,当我们检查元素以在Firebug中查看时,它会显示完整路径) - how to determine an element’s ancestors with jQuery in an iframe (eg. like when we do inspect element to view in firebug it shows the full path) jQuery选择-元素(分离,在Firebug中变暗) - Jquery selection - elements (detached, dimmed out in firebug) 脚本元素不包含文件并且在FIrebug中为空 - Script Elements Not Including Files and Empty in FIrebug 如何在html中创建一个绝对定位的div,该div会将其他元素像苹果的Pages一样推到一边? - How do I create a div in html which is absolutely positioned, which will push other elements to the side like in Apple's Pages?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM