2011-11-10 50 views
0

我努力獲得一些var'ed值到jquery選擇器。在jquery var'ed veiwport寬度/高度插入

// this works below 
    $(".portrait").css({ 
     "width": viewportwidth + 'px', 
     "height": viewportheight + 'px' 
    }); 

,但我不得不搬到從上面的jQuery的CSS選擇離開,而是使用.attr,因爲我需要的!重要的選擇要應用到我的寬度和高度。

我做到這一點的唯一方法,就是使用.attr(我認爲)

// this works below but I can only put a hardcoded value in 
    $('.portrait').attr('style', 'width: 0px !important;'); 


// this is my attempt but fails 
    $('.portrait').attr('style', 'width: viewportwidth + 'px' !important;'); 


// below is exactly what I ideally need to work, multiple styles with !important 
    $('.portrait').attr({ 
     'style', 'width: viewportwidth + 'px' !important;' 
     'style', 'height: viewportheight + 'px' !important;' 
    }); 

// but it does not work cause of my bad coding. 

如果任何人都可以對周圍的工作建議或修正,這將是這樣的幫助。如果你需要我更詳細地解釋讓我知道。

在此先感謝:-)

喬希

+0

你如何做到這一點? - 謝謝,不知道:/ – Joshc

+0

我建議你看看這些 - > http://stackoverflow.com/faq – ManseUK

回答

1

嘗試以下操作:

$(".portrait").css({ 
     "width": viewportwidth + 'px !important', 
     "height": viewportheight + 'px !important' 
    }); 

http://jsfiddle.net/manseuk/5vCab/

.css()樣式/ CSS方法和 .attr()的屬性(標題,SRC ,alt等等)

+0

如何判斷!important在文檔中是否有效?謝謝 – Joshc

+0

應該使用的屬性 – ManseUK

+0

好吧,夥計,謝謝你的幫助! – Joshc