我试图刷新jQuery,所以我买了O'Reillys jQuery食谱。我在使用andSelf()操纵所选元素的父元素的练习中遇到麻烦;尽管我的代码与书籍完全一样。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div>
<p>Paragraph</p>
<p>Paragraph</p>
</div>
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous">
</script>
<script>
$('div').find('p').andSelf().css('border', '1px solid #993300');
</script>
</body>
</html>
据我了解,我的jQuery应该为div添加边框,但是没有。当我删除andSelf()时,它确实向2 p标签添加了边框。难道我做错了什么?
最佳答案
In the jQuery API Documentation:
该API已在jQuery 3.0中删除;采用
.addBack()
相反,应该
一样地工作。
$(function() {
$('div').find('p').addBack().css('border', '1px solid #993300');
});
<div>
<p>Paragraph</p>
<p>Paragraph</p>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>