2016-05-19 16 views
0

我試圖讓這個腳本函數在圖像加載工作,任何線索? 我發現這個計算器劇本,但劇本當初設立的onclick,我想它的onload事件工作或.load我試圖讓這個腳本函數在圖像加載工作

HTML ======

<div id="container"> 
    <div class="box"> 
    <h1>B</h1> 
    <h2>10.35</h2> 
    </div> 

    <div class="box"> 
    <h1>A</h1> 
    <h2>100.05</h2> 
    </div> 

    <div class="box"> 
    <h1>D</h1> 
    <h2>200</h2> 
    </div> 

    <div class="box"> 
    <h1>C<h1> 
    <h2>5,510.25</h2> 
    </div> 
    <img src="http://myscot.com/ImagesMain/myscotLogoResp120.jpg" id="numBnt"/> 
</div> 

JS = =====

var $divs = $("div.box"); 

$("#numBnt").load(function() { 
    var numericallyOrderedDivs = $divs.sort(function (a, b) { 
     return $(a).find("h2").text() > $(b).find("h2").text(); 
    }); 
    $("#container").html(numericallyOrderedDivs); 
}); 

CSS ======

body { 
    background: #eee; 
    font-family: sans-serif; 
} 
.box { 
    background: red; 
    height: 200px; 
    width: 200px; 
} 
.box h1 { 
    color: white; 
    font-size: 3.5em; 
    text-align: center; 
} 
.box h2 { 
    color: black; 
    font-size: 2.5em; 
    text-align: center; 
} 
+0

和..又該腳本呢?什麼不工作? –

+0

看起來像是在'img'標籤上正確使用'.load()' - 出了什麼問題?任何控制檯錯誤? – Starscream1984

+0

.load()有很多缺陷 - 例如,如果圖像已被緩存,則可能會錯過該事件。閱讀.load()的注意事項並確保你不會陷入坑中。 http://api.jquery.com/load-event – RamblinRose

回答

1

我相信你米飛行需要將其包裝在

$(document).ready(function() { 
    console.log("ready!"); 
}); 

看到這個的jsfiddle:https://jsfiddle.net/y1ph42v8/

+0

EXCELLENT var $ divs = $( 「div.box」); $( 「#numBnt」)。一( 「負載」,函數(){ \t的console.log( '加載') \t VAR numericallyOrderedDivs = $ divs.sort(功能(A,B){ \t \t返回$(a).find(「h2」).text()<$(b).find(「h2」)。text(); \t}); \t $(「#container」)。html numericallyOrderedDivs); }); – micalotl

+0

高興我問,你們是偉大的:) – micalotl

+0

不客氣:-) –