I am trying to capture html element like firefox inspector,so I wrote few lines of code in jquery ( http://jsfiddle.net/fbkxj69b/5/ ).
But it is not working for some cases (such as test 1, 5, 6, 7 & 9).
Any suggestions ?
Thanks
Code
jquery
$(document).ready(function () {
$('.ele').mouseenter(function () {
getCaptured(this);
}).mouseleave(function () {
$('.bline').removeAttr('style');
});
});
function getCaptured(obj){
var pos = obj.getBoundingClientRect();
var txt_lleft = ((pos.width - $(obj).textWidth()) / 2) + pos.left;
var txt_rleft = pos.width - txt_lleft + (pos.left * 2);
var top = pos.top + $(window).scrollTop();
var b = 1;
$('#top').css({ top: Math.max(0, top - b), left: txt_lleft - b, width: $(obj).textWidth() + b, height: b });
$('#bottom').css({ top: top + pos.height, left: txt_lleft - b, width: $(obj).textWidth() + b, height: b });
$('#left').css({ top: top - b, left: Math.max(0, txt_lleft - b), width: b, height: pos.height + b });
$('#right').css({ top: top - b, left: txt_rleft, width: b, height: pos.height + (b * 2) });
}
$.fn.textWidth = function () {
var html_org = $(this).html();
var html_calc = '<span id="ipSpnToGetTxtWdth">' + html_org + '</span>';
$(this).html(html_calc);
var width = $(this).find('span#ipSpnToGetTxtWdth').width();
$(this).html(html_org);
return width;
};
html
<div class="ele">test 1</div>
<span class="ele">test 2</span>
<div style="margin-left: 10px;">
<span class="ele">test 3</span>
</div>
<div style="margin-left: 10px;">
<span style="margin-left: 20px;" class="ele">test 4</span>
</div>
<div style="position: absolute; left: 10px; top: 20px;">
<h4 class="ele">test 5</h4>
</div>
<div style="margin-left: 10px; margin-top: 20px;">
<div style="position: absolute; left: 10px; top: 20px;">
<h4 class="ele">test 6</h4>
</div>
</div>
<h4 class="ele">test 7</h4>
<div style="text-align: center; width: 100%;">
<h4 class="ele">test 8</h4>
</div>
<div style="margin-left: 20px; width: 70%;">
<p class="ele">test 9 test 9 test 9 test 9 test 9 test 9 </p>
</div>
<div class="bline" id="top"></div>
<div class="bline" id="bottom" ></div>
<div class="bline" id="left" ></div>
<div class="bline" id="right" ></div>
css
.bline {
background: #000;
position: absolute;
z-index: 1000000;
}
You could use following logic if you want only to 'highlight' element's content (different than chrome browser inspector):
jQuery:
$('.ele').hover(function () {
$(this).contents().wrapAll('<span class="highlight"/>');
/*↑↑↑ could bring invalid HTML if wrapping block element inside SPAN */
}, function () {
$(this).find('.highlight').contents().unwrap()
});
CSS:
.highlight {
outline: 1px solid #000;
}
The possible reason for the rectangle to appear away from the text is because div
is rendered as a block element, but thats not the case with test2
text as it is displyed inside a span
tag which is a inline-block
element.
My suggestion would be to render the div of text test1
as inline-block
element or set the width to the size of the inner text inside div
.
Live code @ Jsfiddle
http://jsfiddle.net/dreamweiver/fbkxj69b/14/
Html Code:
<div style= "width:35px;" class="ele">test 1</div> //set to fixed width
<span class="ele">test 2</span>
MDN Link: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.