2011-03-12 64 views
1

我正在使用twitter feed控件,並且正在嘗試使用javascript調整其中一個元素的字體大小。試圖通過javascript設置標題標記的字體大小

這裏是有問題的HTML片段:

<div class="twtr-hd"> 
    <a class="twtr-profile-img-anchor" href="http://twitter.com/pikefindotcom" target="_blank"><img src="http://a0.twimg.com/profile_images/1266884773/stock-market-chart_normal.jpg" class="twtr-profile-img" alt="profile"></a>      
    <h3>Pikefin</h3>  
     <h4><a href="http://twitter.com/pikefindotcom" target="_blank">pikefindotcom</a></h4> 
</div> 

我試圖改變這個標籤的字體大小:

<h3>Pikefin</h3> 

下面是正顯示出相應的CSS Firebug的輸出:

#twtr-widget-1 .twtr-doc, #twtr-widget-1 .twtr-hd a, #twtr-widget-1 h3, #twtr-widget-1 h4, #twtr-widget-1 .twtr-popular { 
    background-color: #333333 !important; 
    color: #FFFFFF !important; 
} 
index.php #4 (line 1) 
.twtr-widget h3 { 
    font-size: 11px !important; 
    font-weight: normal !important; 
} 
widget.css (line 12) 
.twtr-widget-profile h3, .twtr-widget-profile h4 { 
    margin: 0 0 0 40px !important; 
} 
widget.css (line 12) 

我使用jquery來檢索元素(聲明:jquery noob)這似乎工作正常,但我無法弄清楚設置字體大小的語法。 這就是我認爲的代碼應該是:

var element = $('.twtr-hdr h3'); 
element.style.fontSize="17px"; 

但是,這給了我此錯誤消息: 「element.style是不確定的」

所以我不能確定,究竟什麼類型的對象jquery正在返回。任何援助將不勝感激。

回答

0

你應該使用jQuery的.css

$(".twtr-hdr h3").css({fontSize:"17px"}); 

您的代碼不起作用,因爲jQuery的不返回實際的元素,則返回一個數組...所以:

element[0].style... //this should work too 
0

你應該使用:

var element = $('.twtr-hdr h3').get(0); // this way you'll get the javascript object 
element.style.fontSize="17px"; 

或:

012之前你
3

這應該是,

$('.twtr-hdr h3').css("font-size", "17px");

+0

一樣的2答案。所以我不能在3分鐘內發佈另一個答案。 :(此作品! – hunter 2011-03-12 20:49:19

+0

那是因爲我有小於125個代表

$('.twtr-hdr h3').attr("style", "font-size: 17px"); // jQuery style 
2011-03-12 20:54:03

+0

好吧,有人似乎喜歡它)+1 – hunter 2011-03-12 20:56:32

相關問題