2014-09-30 30 views
0

我想找到一种方法为包装divs的宽度是在他们的图像(特色图像)的大小,所以他们彼此等距。查找图像的宽度和适用于WordPress的包装divs通过JavaScript

我想我可能已经在这里找到了答案:http://www.webmasterworld.com/javascript/3724001.htm(在底部的解决方案),但我不知道如何将它应用到我的网站:http://amuletts.com/barbaragorayska/

像这样的事情也许...?

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'; 
    } 
    } 
    } 
+0

那么......你的尝试是如何实现的? – skrrgwasme 2014-09-30 02:47:00

回答

0

只需使用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> 
+0

谢谢你的帮助。 – amuletts 2014-10-05 22:31:33

0

我问在WordPress的帮助,得到这个JavaScript的这就像一个魅力。

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

articles.each(function() { 

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

}); 

target.css('width', widths); 

我被告知它应该可以用CSS实现。如果我确实设法让它以这种方式工作,我会再次发布。

相关问题