1、修改添加back to top链接的代码,以便这些链接只从第四段后面才开始出现
$(document).ready(function(){ var $p=$('div.chapter p').eq(2).nextAll(); $('<a href="#top">back to top</a>').insertAfter($p); $('<a id="top"></a>').prependTo('body'); })2、在单击back to top链接时,为每个连接后面添加一个新段落,其中包含You were here字样,确保链接仍然有效
$(document).ready(function(){ var $p=$('div.chapter p').eq(2).nextAll(); $('<a href="#top">back to top</a>').insertAfter($p); $('<a id="top"></a>').prependTo('body'); $('a[href$=#top]').click(function(){ $('<p>You were here</p>').insertAfter(this); }) })3、在单击作者名字时,把文本改成粗体(通过添加一个标签,而不是操作类或者CSS属性)
$(document).ready(function(){ var $name=$('div.chapter p a'); $name.click(function(){ $(this).wrap('<b></b>'); }) })4、挑战:在随后单击粗体作者名字时,删除之前添加的<b>元素,也就是在粗体文本与正常文本之间的切换。
$(document).ready(function(){ var $name=$('#f-author'); $name.click(function(){ if($(this).html()=="by Edwin A. Abbott"){ $(this).html('<b>by Edwin A. Abbott</b>'); }else{ $(this).html('by Edwin A. Abbott'); } }) })
5、挑战:为正文中的每个段落添加一个inhabitants类,但不能调用.addClass()方法。确保不影响现有的类。
$(document).ready(function(){ $('div.chapter p').attr({ class:'+= inhabitants' }); })