Element DOM Tree jQuery plugin – Firebug like functionality | RockingCode
Element DOM Tree jQuery plugin – Firebug like functionality | RockingCode
Element DOM Tree jQuery plugin – Firebug like functionality
110Rate this
Have you even seen G+ Feedback page style? It works pretty much like firebug, when you find something working work, you an ordinary user, will point and click which element is buggy so they will know what they need to do.
Here we’ll see a plugin that allows you to do such functionality. It gives you firebug-like selectors but with one really big advantage, it returns the correct element tree, so you’ll know exactly which element is broken. For instance, if you have a bug report on “#content p” you may never know that actually the broken paragraph was the 10th one.
And we’ll learn a little bit about DOM tree and how to get things easily done without messing around with jQuery (when we don’t need it, of course).
Demo & Download
I’ve done a simple test page so you can see how this DOM tree thing works. See the demo or download our sample files so you can easily play and try new things.
DOM tree? What?
No, I’m not talking about this tree.
DOM (Document Object Model) defines a standard way to access and manipulate HTML elements. So window.document means same thing for every browser. It works pretty much like a tree (duh!), where you have parent and children for every element (unless the first and last one, or we’ll enter in an infinite loop).
When you type $(“body”) you are actually saying “Hey jQuery, get this window.document.body element and bring it here in a way that we can manipulate it”. As you can imagine we can actually access and modify things directly via JavaScript, without jQuery. Every “node” stores info about itself and its children, so with a couple of lines we can access precious data about it!
Let’s go through our plugin, now.
HTML & JS
We’ll use jQuery Boilerplate and BootStrap. Both tools help us a lot here, and I think you should be using them
After putting a simple dummy content, we’ll get started with functionality.
Activate & De-activate functions
I think it’s not that good have this thing enabled all the time, so we’ll create a simple button and bind clicks on it:
1<ahref="#"id="elemtree"class="btn">Start debugging!</a>
12345678<script type="text/javascript">$(function(){$("#elemtree").click(function(event){event.preventDefault();$("#container").eltree();});});</script>Now once plugin is activated all other click functions should be disabled. So what we’ll do is to add this to our plugin file:
123456789101112Plugin.prototype.init =function() {varobj =this.obj,$this="",debugDiv ="",debugCss ="",debugJS ="",debug =this.options.debug;obj.addClass("debugging").click(function(event){event.preventDefault();// so links wont be opened while debugging});};As you may notice we have a couple of variables there. One really important is the “debug”, since it is our debugging panel. It’s default value is:
123defaults = {debug:"<div id='eltreeDebug'><span id='eltreeCSSSel'></span><span id='eltreeJSSel'></span><a href='#' class='btn primary' id='eltreeStop'>Stop debugging</a></div>"};So, when you call it you create that bigger panel, CSS panel, JS panel and disable button. Disable function will be the easiest one, we’ll just reload page since disabling binds can be bad in some browsers (try enabling / disabling binds 100 times and you’ll know what I’m talking about).
1234//prepare remove functions$("#eltreeStop").click(function(){window.location.reload();// Instead of unbinding everything, just reload page. Now, IE, you can take a deep breath});Finding the element’s DOM tree
Now to understand our logic you must to know about 2 DOM accessing functions:
- parentNode – When #content is your current element, for instance, you parentNode is “body”. It goes up one level on DOM tree
- childNodes – Again, if your current element is #content childNodes will be all its children (h2, p, blockquote..). It is an array of elements
Our code logic will be:
- User clicks element
- Loop starts; Condition: current element isn’t window.element
- create a parent var
- check for all child nodes of this parent var, which one is our element
- now if this element has id, store it, otherwise if it has class, store it,otherwise just get his tagname
- element’s parent will be our current element
Ok, let’s translate it to JS and put a few other variables to store values that we’ll need:
12345678910111213141516171819202122232425262728293031323334353637383940414243functionselectors(elem) {varcss ="",continueCss = 1,js ="",parent ="",ret =newArray();while(elem !== window.document) {parent = elem.parentNode;//js selectorx=0;while( ( $(parent.childNodes[x])[0] !== elem ) && (x < parent.childNodes.length) ) {x++;}//now we have our childNode!js = x +","+ js;//CSS selectorif(continueCss) {if(elem.id) {css = elem.nodeName +'#'+ elem.id +" "+ css;continueCss = 0;}elseif(elem.className) {css = elem.nodeName +'.'+ elem.className +" "+ css;}else{css = elem.nodeName +" "+ css;}}//let's go up one levelelem = elem.parentNode;}//let's make our js selector usefuljs = (js.slice(0, -1)).split(",");for(x=0; x< js.length; x++) {js[x] ="childNodes["+ js[x] +"]";}js ="window. document. "+ js.join(". ");ret[0] = css.toLowerCase();//cssret[1] = js;//jsreturnret;}Output function
If you’re planning to use it as a feedback system, it’s these lines that you should change. After we have our selectors this function just display them in our debug panels:
123456functionlogThis(elem, css, js ) {varsel = selectors(elem);//if you want to do something else with results (like sending to a feedback plugin) add stuff herecss.text( sel[0] );js.text( sel[1] );}Final result!
This is our final plugin code, check it out:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110/** Project: Rocking Element Tree (a.k.a. eltree)* Description: Adds a firebug-like functionality on page, that gives you element tree and properties by clicking on it* Author: Rochester Oliveira* License: GNU General Public License ( http://en.wikipedia.org/wiki/GNU_General_Public_License )*/// awesome structure from http://jqueryboilerplate.com/;(function( $, window, document, undefined ) {// Create the defaults oncevarpluginName ='eltree',defaults = {debug:"<div id='eltreeDebug'><span id='eltreeCSSSel'></span><span id='eltreeJSSel'></span><a href='#' class='btn primary' id='eltreeStop'>Stop debugging</a></div>"};// The actual plugin constructorfunctionPlugin( element, options ) {this.element = element;this.options = $.extend( {}, defaults, options) ;this._defaults = defaults;this._name = pluginName;this.obj = $(this.element);this.init();}Plugin.prototype.init =function() {varobj =this.obj,$this="",debugDiv ="",debugCss ="",debugJS ="",debug =this.options.debug;//add our debugger box and cachedebugDiv = $(debug).appendTo($("body"));debugCss = debugDiv.children("#eltreeCSSSel");debugJS = debugDiv.children("#eltreeJSSel");//prepare remove functions$("#eltreeStop").click(function(){window.location.reload();// Instead of unbinding everything, just reload page. Now, IE, you can take a deep breath});//now we'll add those logger functionsobj.addClass("debugging").click(function(event){event.preventDefault();// so links wont be opened while debugginglogThis( event.target, debugCss, debugJS );//and let's add this to our logger spans});};functionlogThis(elem, css, js ) {varsel = selectors(elem);//if you want to do something else with results (like sending to a feedback plugin) add stuff herecss.text( sel[0] );js.text( sel[1] );}functionselectors(elem) {varcss ="",continueCss = 1,js ="",parent ="",ret =newArray();while(elem !== window.document) {parent = elem.parentNode;//js selectorx=0;while( ( $(parent.childNodes[x])[0] !== elem ) && (x < parent.childNodes.length) ) {x++;}//now we have our childNode!js = x +","+ js;//CSS selectorif(continueCss) {if(elem.id) {css = elem.nodeName +'#'+ elem.id +" "+ css;continueCss = 0;}elseif(elem.className) {css = elem.nodeName +'.'+ elem.className +" "+ css;}else{css = elem.nodeName +" "+ css;}}//let's go up one levelelem = elem.parentNode;}//let's make our js selector usefuljs = (js.slice(0, -1)).split(",");for(x=0; x< js.length; x++) {js[x] ="childNodes["+ js[x] +"]";}js ="window. document. "+ js.join(". ");ret[0] = css.toLowerCase();//cssret[1] = js;//jsreturnret;}// A really lightweight plugin wrapper around the constructor, preventing against multiple instantiations$.fn[pluginName] =function( options ) {returnthis.each(function() {if(!$.data(this,'plugin_'+ pluginName)) {$.data(this,'plugin_'+ pluginName,newPlugin(this, options ));}});}})(jQuery, window, document);What do you think?
Don’t be shy and share your thoughts with us
Element DOM Tree jQuery plugin – Firebug like functionality | RockingCode的更多相关文章
- How do I pull a native DOM element from a jQuery object? | jQuery Learning Center
How do I pull a native DOM element from a jQuery object? | jQuery Learning Center How do I pull a na ...
- [Javascript] Deep Search nested tag element in DOM tree
// For example you want to search for nested ul and ol in a DOM tree branch // Give example <ol&g ...
- jQuery plugin : bgStretcher 背景图片切换效果插件
转自:http://blog.dvxj.com/pandola/jQuery_bgStretcher.html bgStretcher 2011 (Background Stretcher)是一个jQ ...
- Signs of a poorly written jQuery plugin 翻译 (Jquery插件开发注意事项,Jquey官方推荐)
原文链接:http://remysharp.com/2010/06/03/signs-of-a-poorly-written-jquery-plugin/ 原文作者:remy sharp So far ...
- The ultimate jQuery Plugin List(终极jQuery插件列表)
下面的文章可能出自一位奥地利的作者, 列出很多jQuery的插件.类似的网站:http://jquerylist.com/原文地址: http://www.kollermedia.at/archiv ...
- transfered jquery plugin practice!
jQuery插件开发入门 发表于 2014年05月23日 by 天涯孤雁 被浏览 20,897 次 ======2014-5-27 更新======= Require.js中使用jQuery 插件请查 ...
- JQuery plugin ---- simplePagination.js API
CSS Themes "light-theme" "dark-theme" "compact-theme" How To Use Step ...
- [jQuery] 自做 jQuery Plugin - Part 1
有時候寫 jQuery 時,常會發現一些簡單的效果可以重複利用.只是每次用 Copy & Paste 大法似乎不是件好事,有沒有什麼方法可以讓我們把這些效果用到其他地方呢? 沒錯,就是用 jQ ...
- ollicle.com: Biggerlink – jQuery plugin
ollicle.com: Biggerlink – jQuery plugin Biggerlink – jQuery plugin Purpose Demo Updated for jQuery 1 ...
随机推荐
- mysql学习(四)-字段类型
mysql数据类型: 数值型: 整形:int 浮点型:float double decimal:定点型 日期: date '2012-01-02' time '10:01:01' datetime ...
- java设计模式之 单例模式 Singleton
static 的应用 单例模式 Singleton 单例:保证一个类在系统中最多只创建一个实例. 好处:由于过多创建对象实例,会产生过多的系统垃圾,需要GC频繁回收,由于GC会占用较大的系统资源,所有 ...
- 通过Orchard认识的Autofac
反射Reflection 这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:'程序集(Assembly)'.'模块(Module)'.'类型(class)'组成,而反射提供一种编程 ...
- SQL Server 向堆表中插入数据的过程
堆表中 IAM 记录着的数据页,表的各个数据页之间没有联系.也就是说一个页面它不会知道自己的前一页是谁,也不知道自己的后一页是谁. 插入数据时先找到IAM页,再由pfs(page free spac ...
- centeros resin安装脚本启动
公司用的服务器是resin+apache #! /bin/sh # # See contrib/init.resin for /etc/rc.d/init.d startup script # # r ...
- Android程序的入口点
原文:Android程序的入口点 android应用程序,由一到多个Activity组成.每个Activity没有很紧密的联系,因为我们可以在自己的程序中调用其它Activity,特别是调用自己的代码 ...
- windows设置临时环境变量path
所有在命令行下对环境变量的修改只对当前窗口有效,不是永久性的修改. 1.查看当前所有可以的环境变量:输入set查看 2.查看某个环境变量:输入 set 变量名 例如 set path 3.修改环境变量 ...
- 给Object扩展新方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Count the Colors(线段树,找颜色段条数)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- WPF基础——继承
1) 可以定义继承自其他类的类,关系是“父类/子类”.子类继承父类的成员(不严谨) 2) 定义语法“class子类:父类”,不指定父类则父类为Object,.Net中所有类都直接或者间接继承自Ob ...

