官术网_书友最值得收藏!

  • MooTools 1.3 Cookbook
  • Jay Larry G. Johnston
  • 217字
  • 2021-04-02 19:07:09

Styling the text within an element

We are often faced with a reason to style text on-the-fly using client-side code. MooTools makes that quite elementary with concepts found prominent in this very recipe!

Getting ready

Prepare to make the text in our SPAN stand out: by making it bold.

How to do it...

It is possible to set the styles of any text element quickly and easily.

<script type="text/javascript" src="mootools-1.3.0.js"></script>
</head>
<body>
<form action="" method="get">
<input id="go_bold" type="button" value="Bold Oscar"/>
<input id="go_div" type="button" value="Set Oscar Apart"/>
</form>
<br/>
<div>Every great man nowadays has his disciples,
and it is always Judas who writes the biography.
<span id="quote_author">Oscar Wilde</span></div>
<script type="text/javascript">
$('go_bold').addEvent('click', function() {
// set a style property of an element
$('quote_author').setStyle('font-weight','bold');
});
$('go_div').addEvent('click', function() {
// use an object to set many style properties at once
$('quote_author').setStyles({
'display': 'block',
'text-decoration': 'underline',
'font-style': 'italic',
'font-size': '18px',
'margin': '5px 0 0 5px'
});
});
</script>

How it works...

The Element.setStyle() method takes two parameters, the style property to alter and the new value for the property. In this example, we also use Element.setStyles(), which will take an object that can alter many style properties in one fell swoop.

There's more...

The CSS style specification is huge; imagine the power that we have on-the-fly when we can change styles of any element on the page, most particularly in response to something the client user has done. Now we have interaction and style in doing it!

主站蜘蛛池模板: 洛隆县| 咸宁市| 桐城市| 泸州市| 修水县| 唐海县| 巴塘县| 本溪| 太仓市| 南召县| 米脂县| 兴隆县| 游戏| 军事| 正定县| 海门市| 海淀区| 易门县| 湛江市| 邹城市| 新安县| 乳源| 淳安县| 静海县| 蓬莱市| 水城县| 新巴尔虎右旗| 常德市| 麻栗坡县| 云霄县| 瓦房店市| 古丈县| 太保市| 通山县| 德保县| 佛坪县| 都江堰市| 长治市| 峨边| 绵阳市| 淮北市|