试图通过javascript设置标题标记的字体大小
问题描述:
我正在使用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正在返回。任何援助将不胜感激。
答
你应该使用jQuery的.css
:
$(".twtr-hdr h3").css({fontSize:"17px"});
您的代码不起作用,因为jQuery的不返回实际的元素,则返回一个数组...所以:
element[0].style... //this should work too
答
你应该使用:
var element = $('.twtr-hdr h3').get(0); // this way you'll get the javascript object
element.style.fontSize="17px";
或:
012之前你
答
这应该是,
$('.twtr-hdr h3').css("font-size", "17px");
一样的2答案。所以我不能在3分钟内发布另一个答案。 :(此作品! – hunter 2011-03-12 20:49:19
那是因为我有小于125个代表
– 2011-03-12 20:54:03好吧,有人似乎喜欢它)+1 – hunter 2011-03-12 20:56:32