我想重新了解 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 :
This API has been removed in jQuery 3.0; use
.addBack()
instead, which should work identically.
$(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>
关于jquery - 在阅读 O'Reillys jQuery Cookbook 时遇到 andSelf() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60953433/