[英]onhover how to get the image width and height using jquery
I am displaying in my webpage a number of images within a div the div and images are created dynamically with a looping. 我在网页中显示div内的许多图像,并且这些图像是通过循环动态创建的。
I need to get the width and height of the each image by using the jquery without using id like this 我需要通过使用jQuery而不是这样的ID来获取每个图像的宽度和高度
document.getElementById('Image/div id');
because there will a lot of images dynamically created by the loop depend upon the conditions so, is there any way to get the height and width of the image when user hover/click the image 因为循环会根据条件动态创建很多图像,所以当用户悬停/单击图像时,是否有任何方法可以获取图像的高度和宽度
I struck with this for a long and been here finally hopes i get a solution 我为此打了很长一段时间,终于在这里希望我能找到解决方案
I'd suggest, if you want to show this information on the page: 我建议,如果您想在页面上显示此信息:
$('img').hover(
function(){
var h = $(this).height(),
w = $(this).width();
$('<div />').insertAfter($(this)).text('height: ' + h + '; width: ' + w +'.');
},
function(){
$(this).next('div').remove();
});
Almost pointless edit to make it a little bit prettier by reducing the calls to $(this)
and coupling it to some CSS: 通过减少对$(this)
的调用并将其耦合到某些CSS, 几乎没有意义的编辑使其更加漂亮。
$('img').hover(
function(){
var that = $(this),
h = that.height(),
w = that.width();
$('<div />')
.css('width',w)
.text('height: ' + h + '; width: ' + w +'.')
.insertAfter(that);
},
function(){
$(this).next('div').remove();
});
CSS: CSS:
div {
display: block;
position: relative;
}
div > div {
position: absolute;
top: 0;
left: 0;
color: #f90;
background-color: #000;
background-color: rgba(0,0,0,0.6);
}
JS Fiddle demo . JS提琴演示 。
Edited because jQuery's not really necessary for this effect (albeit it does simplify the implementation), so: a plain JavaScript alternative: 进行了修改,因为jQuery并非真正需要这种效果(尽管它确实简化了实现),因此:一种普通的JavaScript替代方案:
var img = document.getElementsByTagName('img');
for (var i=0,len=img.length;i<len;i++){
img[i].onmouseover = function(){
var that = this,
h = that.offsetHeight,
w = that.offsetWidth,
p = that.parentNode,
d = document.createElement('div');
d.style.width = w + 'px';
d.textContent = 'Width: ' + w + '; height: ' + h + '.';
p.appendChild(d);
};
img[i].onmouseout = function(){
var that = this;
that.parentNode.removeChild(that.nextSibling);
};
}
JS Fiddle demo . JS提琴演示 。
Final edit (I think) , because I couldn't remember the compatibility for node.textContent
, I thought this amendment might aid compatibility with lower versions of IE (using document.createTextNode()
instead of relying on node.textContent
/ node.innerText
and so on...): 最终编辑(我认为) ,因为我不记得与node.textContent
的兼容性, node.textContent
我认为此修订可能有助于与较低版本的IE兼容(使用document.createTextNode()
而不是依赖于node.textContent
/ node.innerText
等等...):
var img = document.getElementsByTagName('img');
for (var i=0,len=img.length;i<len;i++){
img[i].onmouseover = function(){
var that = this,
h = that.offsetHeight,
w = that.offsetWidth,
p = that.parentNode,
d = document.createElement('div'),
text = document.createTextNode('Width: ' + w + '; height: ' + h + '.');
d.appendChild(text);
d.style.width = w + 'px';
p.appendChild(d);
};
img[i].onmouseout = function(){
var that = this;
that.parentNode.removeChild(that.nextSibling);
};
}
JS Fiddle demo . JS提琴演示 。
While I don't have IE 7, or lower, the above does work in IE 8 at least. 虽然我没有IE 7或更低版本,但以上至少在IE 8中确实有效。 If anyone has comments about functionality in IE 6 or 7 I'd be interested..! 如果有人对IE 6或7中的功能有任何评论,我将很感兴趣。
References: 参考文献:
'Plain' JavaScript: 普通JavaScript:
document.createElement
. document.createElement
。 document.createTextNode()
. document.createTextNode()
。 element.offsetHeight
. element.offsetHeight
element.offsetWidth
. element.offsetWidth
。 element.style
. element.style
。 node.appendChild
. node.appendChild
。 node.parentNode
. node.parentNode
。 node.removeChild
. node.removeChild
。 node.textContent
. node.textContent
。 jQuery: jQuery的:
you can use jQuery on()
to attach a handler to the nearest common parent container . 您可以使用jQuery on()
将处理程序附加到最近的公共父容器 。 that way, you can at least control which subset of images you want this function to take effect. 这样,您至少可以控制要此功能生效的图像子集。
$('#container').on('mouseover','img',function(){
var width = $(this).width();
var height = $(this).height();
});
like: 喜欢:
<div>
<img> //will not affect this one since it's not under "#container"
</div>
<div id="container">
<img> //the handler affects this one
<div>
<img> //the handler also affects this one
</div>
</div>
Does this solve your issue? 这样可以解决您的问题吗?
$('img').hover(function() {
console.log($(this).width());
console.log($(this).height());
});
Use $(this)
to refer to the image being hovered. 使用$(this)
引用要悬停的图像。
$("#div_id").hover(function(){
alert("H:" + $(this).height() + " W:" + $(this).width() );
});
$(".img").mouseover(function() {
var $div = $(this);
var $item = $div.find("img");
var width = $item.width();
var height = $item.height();
}
try this. 尝试这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.