(4)事件处理——(9)让其他按钮可用(Enabling the other buttons)

We now have a Large Printbutton that works as advertised, but we need to apply similar handling to the other two buttons (Defaultand Narrow Column) to make them perform their tasks. This is straightforward; we use .bind()to add a clickhandler to each of them, removing and adding classes as necessary. The new code reads as shown in the following code snippet:

$(document).ready(function() {
$('#switcher-default').bind('click', function() {
$('body').removeClass('narrow');
$('body').removeClass('large');
});
$('#switcher-narrow').bind('click', function() {
$('body').addClass('narrow');
$('body').removeClass('large');
});
$('#switcher-large').bind('click', function() {
$('body').removeClass('narrow');
$('body').addClass('large');
});
});

This is combined with a CSS rule for the narrowclass:

body.narrow .chapter {width: 250px;}
我们现在有了一个像之前介绍的那样工作的大号字体按钮,现在我们需要为另外两个按钮(默认和窄间距按钮)添加相似的处理器,让他们可以完成自己的任务。这次很直接,我们使用.bind()为他们都添加一个click处理器,移去和添加类。新的代码像下面的代码片段一样:
$(document).ready(function() {
$('#switcher-default').bind('click', function() {
$('body').removeClass('narrow');
$('body').removeClass('large');
});
$('#switcher-narrow').bind('click', function() {
$('body').addClass('narrow');
$('body').removeClass('large');
});
$('#switcher-large').bind('click', function() {
$('body').removeClass('narrow');
$('body').addClass('large');
});
});

这些和为narrow 类设计的css规则一起使用:
body.narrow .chapter {width: 250px;}
Now, after clicking the Narrow Columnbutton, its corresponding CSS is applied and the text gets laid out differently:

Clicking on Defaultremoves both class names from the <body>tag, returning the page to its initial rendering.

现在点击窄行间距按钮后,对应的css将会被应用,然后文本将会变得不同:


点击默认按钮,将移去body标签上的两个类,染回网页初始渲染的样子。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值