jQuery基础教程(第四版)第3章练习:

关于答案:

// // (1) 在Charles Dickens被单击时,给它应用 selected 样式。
$(document).ready(function() {
$(‘#header‘).on(‘click‘,function() {
$(‘.author‘).addClass(‘selected‘)
});
})

// (2) 在双击章标题( <h3 class="chapter-title"> )时,切换章文本的可见性。
$(document).ready(function() {
$(‘#chapter-preface‘).on(‘dblclick‘,function() {
$(‘.chapter-title‘).toggleClass(‘hidden‘);
})
})

// (3) 当用户按下向右方向键时,切换到下一个 body 类;右方向键的键码是 39 。
// var setBodyClass=function(className){
// $("body").removeClass().addClass(className);
// $(".switcher button").removeClass("selected");
// $("#switcher-"+className).addClass("selected");
// }
// //页面初始时默认为选中状态
// $("#switcher-default").addClass("selected");
// $(document).keyup(function (event) {
// var num=$(".switcher button").filter(".selected").index()-1;
// alert(num);
// if(event.which==39) num=(num+1)%3;
// else if(event.which==37) num=(num-1)%3;
// else return false;
// var btnClassname=$(".switcher button").eq(num).attr("id").split("-")[1];
// setBodyClass(btnClassname);
// })

// 第二种方法
$(function(){
var setBodyClass=function(className){
$("body").removeClass().addClass(className);
$(".switcher button").removeClass("selected");
$("#switcher-"+className).addClass("selected");
}
$("#switcher-default").addClass("selected");//页面初始时默认为选中状态
var bodyClass=["default","narrow","large"];
var count=0;//设置一个计数器
$(document).keyup(function(event){
if (event.keyCode == 39) {
setBodyClass(bodyClass[(count+1)%3]);//如果设置为count%3则在第一次按下右键时页面不会有反应(因为默认是setBodyClass[0])
count++;
}
})
})

// (4) 挑战:使用 console.log() 函数记录在段落中移动的鼠标的坐标位置。(注意: console.log()可以在Firefox的firebug扩展、Safari的Web Inspector或Chrome、IE中的Developer Tools中使用。)
//获取鼠标坐标函数
$(document).ready(function(){
//获取鼠标坐标函数
$(document).mouseover(function(e) {
         var x = e.pageX;
         var y = e.pageY;
console.log(‘x坐标‘+x+‘,‘+‘y坐标‘+y);
})
})

// (5) 挑战:使用 .mousedown() 和 .mouseup() 跟踪页面中的鼠标事件。如果鼠标按键在按下它的地方被释放,则为所有段落添加 hidden类。如果是在按下它的地方之下被释放的,删除所有段落的 hidden 类。
$(document).ready(function(){
       varposxr=0,posy=0;
$(document).mousedown(function(event){
      posx=event.clientX;
      posy=event.clientY;
})
$(document).mouseup(function(event){
     posx1=event.clientX;
     posy1=event.clientY;
     if(posy1==posy&&posx==posx1){
              $(‘p‘).addClass(‘hidden‘)
}
else if(posy1>posy){
$(‘p‘).removeClass(‘hidden‘)
}
})
})

书本配套的源代码HTML:

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<title>A Christmas Carol</title>

<link rel="stylesheet" href="css/03.css" type="text/css" />

<script src="jquery.js"></script>
<script src="js/03.js"></script>
</head>
<body>
<div id="container">

<div id="switcher" class="switcher">
<h3>Style Switcher</h3>
<button id="switcher-default">
Default
</button>
<button id="switcher-narrow">
Narrow Column
</button>
<button id="switcher-large">
Large Print
</button>
</div>

<div id="header">
<h2>A Christmas Carol</h2>
<h2 class="subtitle">In Prose, Being a Ghost Story of Christmas</h2>
<div class="author">by Charles Dickens</div>
</div>

<div class="chapter" id="chapter-preface">
<h3 class="chapter-title">Preface</h3>
<p>I HAVE endeavoured in this Ghostly little book, to raise the Ghost of an Idea, which shall not put my readers out of humour with themselves, with each other, with the season, or with me. May it haunt their houses pleasantly, and no one wish to lay it.</p>
<p>Their faithful Friend and Servant,</p>
<p>C. D.</p>
<p>December, 1843.</p>
</div>

<div class="chapter" id="chapter-1">
<h3 class="chapter-title">Stave I: Marley‘s Ghost</h3>
<p>MARLEY was dead: to begin with. There is no doubt whatever about that. The register of his burial was signed by the clergyman, the clerk, the undertaker, and the chief mourner. Scrooge signed it: and Scrooge‘s name was good upon ‘Change, for anything he chose to put his hand to. Old Marley was as dead as a door-nail.</p>
<p>Mind! I don‘t mean to say that I know, of my own knowledge, what there is particularly dead about a door-nail. I might have been inclined, myself, to regard a coffin-nail as the deadest piece of ironmongery in the trade. But the wisdom of our ancestors is in the simile; and my unhallowed hands shall not disturb it, or the Country‘s done for. You will therefore permit me to repeat, emphatically, that Marley was as dead as a door-nail.</p>
<p>Scrooge knew he was dead? Of course he did. How could it be otherwise? Scrooge and he were partners for I don‘t know how many years. Scrooge was his sole executor, his sole administrator, his sole assign, his sole residuary legatee, his sole friend, and sole mourner. And even Scrooge was not so dreadfully cut up by the sad event, but that he was an excellent man of business on the very day of the funeral, and solemnised it with an undoubted bargain.</p>
<p>The mention of Marley‘s funeral brings me back to the point I started from. There is no doubt that Marley was dead. This must be distinctly understood, or nothing wonderful can come of the story I am going to relate. If we were not perfectly convinced that Hamlet‘s Father died before the play began, there would be nothing more remarkable in his taking a stroll at night, in an easterly wind, upon his own ramparts, than there would be in any other middle-aged gentleman rashly turning out after dark in a breezy spot&mdash;say Saint Paul‘s Churchyard for instance&mdash; literally to astonish his son‘s weak mind.</p>
<p>Scrooge never painted out Old Marley‘s name. There it stood, years afterwards, above the warehouse door: Scrooge and Marley. The firm was known as Scrooge and Marley. Sometimes people new to the business called Scrooge Scrooge, and sometimes Marley, but he answered to both names. It was all the same to him.</p>
<p>Oh! But he was a tight-fisted hand at the grind-stone, Scrooge! a squeezing, wrenching, grasping, scraping, clutching, covetous, old sinner! Hard and sharp as flint, from which no steel had ever struck out generous fire; secret, and self-contained, and solitary as an oyster. The cold within him froze his old features, nipped his pointed nose, shrivelled his cheek, stiffened his gait; made his eyes red, his thin lips blue; and spoke out shrewdly in his grating voice. A frosty rime was on his head, and on his eyebrows, and his wiry chin. He carried his own low temperature always about with him; he iced his office in the dog-days; and didn‘t thaw it one degree at Christmas.</p>
<p>External heat and cold had little influence on Scrooge. No warmth could warm, no wintry weather chill him. No wind that blew was bitterer than he, no falling snow was more intent upon its purpose, no pelting rain less open to entreaty. Foul weather didn‘t know where to have him. The heaviest rain, and snow, and hail, and sleet, could boast of the advantage over him in only one respect. They often "came down" handsomely, and Scrooge never did.</p>
<p>Nobody ever stopped him in the street to say, with gladsome looks, "My dear Scrooge, how are you? When will you come to see me?" No beggars implored him to bestow a trifle, no children asked him what it was o‘clock, no man or woman ever once in all his life inquired the way to such and such a place, of Scrooge. Even the blind men‘s dogs appeared to know him; and when they saw him coming on, would tug their owners into doorways and up courts; and then would wag their tails as though they said, "No eye at all is better than an evil eye, dark master!"</p>
<p>But what did Scrooge care! It was the very thing he liked. To edge his way along the crowded paths of life, warning all human sympathy to keep its distance, was what the knowing ones call "nuts" to Scrooge.</p>
</div>
</div>
</body>
</html>

书本配套的CSS:

/***************************************
Default Styles
************************************** */

html, body {
margin: 0;
padding: 0;
}

body {
font: 62.5% Verdana, Helvetica, Arial, sans-serif;
color: #000;
background: #fff;
}
#container {
font-size: 1.2em;
margin: 10px 2em;
}

h1 {
font-size: 2.5em;
margin-bottom: 0;
}

h2 {
font-size: 1.3em;
margin-bottom: .5em;
}
h3 {
font-size: 1.1em;
margin-bottom: 0;
}

code {
font-size: 1.2em;
}

a {
color: #06581f;
}

/***************************************
Chapter Styles
************************************** */

.poem {
margin: 0 5em;
}
.chapter {
margin: 1em;
}
.switcher {
float: right;
background-color: #ddd;
border: 1px solid #000;
margin: 10px;
padding: 10px;
font-size: .9em;
}
.switcher h3 {
margin: .5em 0;
}

#header {
clear: both;
}

body.large .chapter {
font-size: 1.5em;
}

body.narrow .chapter {
width: 250px;
}

.selected {
font-weight: bold;
}

.hidden {
display: none;
}

.hover {
cursor: pointer;
background-color: #afa;
}

有些是借鉴他人博客.如有影响,侵删.

原文地址:https://www.cnblogs.com/qinghui258/p/8424653.html

时间: 2024-11-11 12:19:29

jQuery基础教程(第四版)第3章练习:的相关文章

Ajax本地跨域问题 Cross origin requests are only supported for HTTP(针对jQuery基础教程第四版第六章)

出现的问题: 解决的步骤: 谷歌浏览器出现的效果: 针对jQuery基础教程(第四版),第六章  成功: 原文地址:https://www.cnblogs.com/qinghui258/p/8432569.html

jQuery基础教程(第四版)第4章练习:

关于答案: // (1) 修改样式表,一开始先隐藏页面内容,当页面加载后,慢慢地淡入内容:$(document).ready(function() { $('body').hide().fadeIn(4000);}) // (2) 在鼠标悬停到段落上面时,给段落应用黄色背景:$(document).ready(function() { $('p').mousemove(function(event) { //鼠标进入的时候 $(this).css('background','yellow');

《jQuery基础教程(第四版)》学习笔记

第2章 选择元素 1. 使用$()函数 $()函数其实是创建了一个jQuery对象. 这个函数接受CSS选择符作为参数,充当一个工厂, 返回包含页面中对应元素的jQuery对象. 所有能在样式表中使用的选择符都可以传给这个函数, 随后就可以对匹配的元素集合应用jQuery方法. 在jQuery中,美元符号$其实就是标示符jQuery的"别名". 2. 选择符 1. 基本选择符 $('p') //取得所有标签为p的元素 $('.class') //取得所有类为class的元素 $('#i

笔记-Python基础教程(第二版)第一章

第一章 快速改造:基础知识 01:整除.乘方 (Python3.0之前 如2.7版本) >>> 1/2 ==>0 1/2整除,普通除法: 解决办法1: 1.0/2.0  ==>0.5 解决办法2:from _future_ import division 1/2  ==>0.5 // 表示整除 >>>1//2 ==>0 浮点数//浮点数,结果依然是整除结果 >>>1.0//2.0 ==>0.0 实际操作截图: ** 表示乘

python基础学习笔记——Python基础教程(第2版 修订版)第一章

#模块 import math math.floor(9) from math import sqrt sqrt(9) #无需使用前缀 import cmath cmath.sqrt(-1) #不能使用from...inport #转义和单双引号 >>>"\"hello,word\"she said" '"hello,word"she said' #使用print不显示引号 #拼接字符串  + #输入 input raw_inp

python基础学习笔记——Python基础教程(第2版 修订版)第二章(列表和元祖)

#列表可修改,元祖不能 A=['sdsd',43] B=['sds',45] C=[A,B] #分片 : - #list函数 #分片赋值 #列表方法 lst.append(4) x.count(1) x.count([1,2]) a.extend(b) a.index("w") a.insert(3,"都")x.removex.reversex.sort #pop 移除列表元素,并返回值.实现数据结构-栈,LIFO(后进先出),x.append(x.pop()),先

《jQuery基础教程》读书笔记

最近在看<jQuery基础教程>这本书,做了点读书笔记以备回顾,不定期更新. 第一章第二章比较基础,就此略过了... 第三章 事件 jQuery中$(document).ready()与javascript原生的window.onload()区别: $(document).ready():通过该方法注册的事件处理程序,会在DOM完全就绪并使用时调用.虽然这意味着所有元素对脚本而言都是可访问的,但是,却不意味着所有关联的文件都下载完毕.当HTML下载 完成并解析成DOM树之后,代码就可以开始运行

【jquery基础教程】jquery事件及操作大汇总

在学习Javascript语言中,有一个优秀的Javascript库是大家都会遇到的--jquery,今天小编汇总了jquery事件及操作,现在一起来看看jquery基础教程吧! 事件 bind()        向匹配元素附加一个或更多事件处理器 blur( )        触发.或将函数绑定到指定元素的 blur 事件 change()        触发.或将函数绑定到指定元素的 change 事件 click()        触发.或将函数绑定到指定元素的 click 事件 dblc

《python基础教程(第二版)》学习笔记 字符串(第3章)

<python基础教程(第二版)>学习笔记 字符串(第3章)所有的基本的序列操作(索引,分片,乘法,判断成员资格,求长度,求最大最小值)对字符串也适用.字符串是不可以改变的:%左侧是格式字符串,右侧是需要格式化的值print '%s=%d' % ('x',100) ==> x=100%% 格式字符串中出现 %模板字符串:from string import Templates=Template('$x is 100');  s.substitute(x='ABC');  ==> '