jQuery Add() 不起作用

标签 jquery

我正在尝试使用 add() 构建一个 jQuery 集,但我的实现并未向该集中添加元素。

 var questionPath = $(); // create empty set
    var selectedRuleElement = $(event.currentTarget);
    questionPath.add(selectedRuleElement); //no element is added to set here!
    selectedRuleElement.find('li.property-value-node').each(function () {
        var questionId = $(this).attr('data-parent-id');
        questionPath.add($('#' + questionId)); // or here!
    });
    return questionPath;

上面的两行 add() 行都给出了一个有效元素作为参数。有人可以告诉我向 jQuery 集合添加元素的正确方法吗?

最佳答案

与字符串一样,jQuery 对象是不可变的,因此您需要重新分配返回值。否则,您将丢弃 .add() 返回的值,并且 questionPath 保持不变。

questionPath = questionPath.add(selectedRuleElement);

关于jQuery Add() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11636336/

相关文章:

javascript - 更改 Flowplayer 中的播放图标

jquery - iframe 覆盖内容

javascript - 如何更改图像 src onclick?播放/暂停

javascript - 我如何在 javascript 中编写以下 php 代码?

javascript - 将剪切的 contenteditable div 的 View 移动到光标位置

c# - Jquery + AJAX + ASP.Net + WebForms

javascript - 发出 JSON RPC API 的 GET 请求而不是 POST 请求

javascript - Ajax 调用后 HTML 返回原始状态

javascript - 更加模块化;在 .js 文件中包含 html 元素?

javascript - 如何将 Javascript 和 CSS 包含到 Bootstrap 灯箱模式对话框中?