Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找图像宽度&;通过Javascript应用于wordpress中的包装div_Javascript_Jquery_Wordpress_Image_Width - Fatal编程技术网

查找图像宽度&;通过Javascript应用于wordpress中的包装div

查找图像宽度&;通过Javascript应用于wordpress中的包装div,javascript,jquery,wordpress,image,width,Javascript,Jquery,Wordpress,Image,Width,我想找到一种方法,使包装器divs的宽度与其中的图像(特征图像)的大小相同,因此它们之间的距离相等 我想我可能已经在这里找到了答案:(底部的解决方案),但我不确定如何将其应用到我的网站: 像这样的可能 window.onload=function() { setCaptions(); } function setCaptions() { var picwidth=[]; var containerDiv=document.getElementBy

我想找到一种方法,使包装器divs的宽度与其中的图像(特征图像)的大小相同,因此它们之间的距离相等

我想我可能已经在这里找到了答案:(底部的解决方案),但我不确定如何将其应用到我的网站:

像这样的可能

    window.onload=function() {
    setCaptions();
    }

    function setCaptions() {
    var picwidth=[];
    var containerDiv=document.getElementById('#primary-home');
    var dvs=containerDiv.getElementsByTagName('div');
    for(var c=0;c<dvs.length;c++) {
    if(dvs[c].id=='homepage-article .featured-image') {
    picwidth[c]=dvs[c].getElementsByTagName('img')[0].clientWidth;
    dvs[c].style.width=picwidth[c]+'px';
    }
    }
    }
window.onload=function(){
设置字幕();
}
函数setCaptions(){
var picwidth=[];
var containerDiv=document.getElementById(“#主主页”);
var dvs=containerDiv.getElementsByTagName('div');
对于(var c=0;c只需使用jQuery:

<script type="text/javascript">
jQuery( document ).ready(function($) {
  $('.article-wrapper').each(function(){
    var width = $(this).find('img').width;
    $(this).css('width',width+'px');
    var height = $(this).find('img').height;
    $(this).css('height',height+'px');
  });
)};
</script>  

jQuery(文档).ready(函数($){
$('.article wrapper')。每个(函数(){
变量宽度=$(this).find('img').width;
$(this.css('width',width+'px');
变量高度=$(this).find('img').height;
$(this.css('height',height+'px');
});
)};

我向wordpress寻求帮助,得到了这个javascript,它工作起来很有魅力

var $ = jQuery,
articles = $('.article-wrapper'),
widths = 0,
target = $('#primary-home');

articles.each(function() {

widths += $(this).width();

 });

target.css('width', widths);

有人告诉我,使用CSS应该可以实现。如果我真的能做到这一点,我会再次发布。

那么……你的尝试效果如何?谢谢你的帮助。