2010-02-02 9 views
9

C'è un modo per impostare l'altezza e la larghezza di un'immagine usando jquery? Quello che segue è il mio codiceCome impostare l'altezza, la larghezza dell'immagine usando jquery

var img = new Image(); 
// Create image 
$(img).load(function(){     
    imgdiv.append(this); 
}).error(function() { 
    $('#adsloder').remove(); 
}).attr({ 
    id: val.ADV_ID, 
    src: val.ADV_SRC, 
    title: val.ADV_TITLE, 
    alt: val.ADV_ALT 
}); 

Grazie.

risposta

19

è possibile chiamare il .height() e, .width() setter:

var img = new Image(); 
// Create image 
$(img).load(function(){     
    imgdiv.append(this); 
}).error(function() { 
    $('#adsloder').remove(); 
}).attr({ 
    id: val.ADV_ID, 
    src: val.ADV_SRC, 
    title: val.ADV_TITLE, 
    alt: val.ADV_ALT 
}).height(100).width(100); 
+0

ringrazia Nick – Vicky

+0

'$ ('', {id: val.ADV_ID, src : val.ADV_SRC, titolo: val.ADV_TITLE, alt: val.ADV_ALT}). load (function() {imgdiv.append (this);}). height (100) .width (100); ' – Dementic

+0

@Dementic - in questo caso per alcuni browser è imp ortant che il gestore di carico è specificato prima della fonte, in modo che i carichi istantanei da cache siano gestiti in tutti i casi ... quindi anche se più corto, non è corretto in tutti i casi. Certo, con Opera spostarsi su webkit è meno di un problema al giorno d'oggi. –

5
$(".img1").css('height','10'); 
$(".img1").css('width','10'); 

O

$(".img1").attr('height','10'); 
$(".img1").attr('width','10'); 
+1

o '$ ('. Img1'). Css ({'height': '10px', 'width': '10px'});' – Dementic

Problemi correlati