jquery - 在阅读 O'Reillys jQuery Cookbook 时遇到 andSelf() 问题

标签 jquery

我想重新了解 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/

相关文章:

javascript - 如何处理主干js中特定目标的点击事件

javascript - 如何使用 keyup 在 javascript 中读取值并验证密码长度?

javascript - CSS JS 日期格式问题

c# - jquery ajax调用返回值

javascript - 鼠标悬停效果仅在 <a> 中的 <i> 上

jquery - 仅在提交后显示 jQuery.validationEngine 警报

php - 使用 jQuery 或 Ajax get 函数从另一个页面提取文本

javascript - 替换每个在 jQuery 中不起作用

jquery - 使用 JQuery 从 IE 中的输入电话字段中删除 "X"

javascript - 在循环数组时使用 jQuery .get() 前置的元素在页面刷新时以不同的顺序加载