2014-09-30 28 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實現。如果我確實設法讓它以這種方式工作,我會再次發佈。

相關問題