![](/img/trans.png)
[英]Limit the browser windows height can not less than 200 px using Jquery?
[英]First image in <body> greater than 200 px Using Jquery
对于社交媒体共享,我尝试使用jQuery获得大于200像素的第一个图像URL。 这是我的代码不起作用:
$("body img").each(function() {
var $minHeight = 200;
if ($(this).height() > $minHeight) {
$(this).attr('src')
}
});
获得正确的图像后,我需要在下面的content="default-image.png"
中更改默认图像URL:
<meta property="og:image" name="twitter:image" content="default-image.png"/>
这是我发现的元标记的有效选择器:
$('meta[property=og\\:image]').attr('content','new-image.png');
我发现我的代码有效,但是...
$("body img").each(function() {
var $minHeight = 200;
if ($(this).height() < $minHeight) {
$x = $(this).attr('src')
}
});
$('meta[property=og\\:image]').attr('content',$x);
$(".share a[href*='pinterest']").prop("href","https://pinterest.com/pin/create/button/?url=http://mywebsite.com/mypage&media="+$x);
但是if ($(this).height() > $minHeight)
, if ($(this).height() > $minHeight)
上述脚本无法按需使用大于号
因此,当您执行$(“ body img”)时,jquery返回一个元素数组。
如果我理解正确,那么您需要的图像应该是ACTUAL HEIGHT,但不是CSS提供的高度。 在这种情况下:
var ImagesGreaterThan200Height =jQuery.grep($("body img"), function( img, i ) {
if(img.naturalHeight > 200)
return img;
});
从上面的代码中,您将获得一个高度大于200px的图像数组,对于第一个图像,您只需要执行以下操作:
ImagesGreaterThan200Height[0] // This returns the first Image
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.