前言:利用了jQuery的一些API
$(function () {
var $p = $('div.chapter p').eq(3);
$p
//(1)chapter类下的第4个p标签,之前的所有a标签全部移除
.prevAll('a').remove()
//(2)单击back to top,在下面添加新段落
.nextAll('a').click(function () {
$(this).after('<p>You were here</p>')
});
//(3)(4)单击作者名添加或删除加粗标签
var $author = $('#f-author');
$author
.click(function () {
var $parent = $(this).parent();
if ($parent.is('b')) return $(this).unwrap();
$(this).wrap('<b></b>')
})
//(5)不用addClass添加一个类,不允许影响之前的className
$('p').attr('class', function () {
//获取p当前的className,利用字符串拼接,返回这个字符串
return this.className + ' ' + 'inhabitants'
})
})