gpt4 book ai didi

javascript - 自定义 jQuery 函数 - 选择器找不到元素

转载 作者:行者123 更新时间:2023-11-28 16:12:08 26 4
gpt4 key购买 nike

我正在尝试制作 REST api,并且正在研究一些 javascript 函数。
这里的想法是运行例如: $('#main').get('car/ford'); 并且返回的数据将添加到提供的元素中。

这是所有的 javascript:

$.fn.extend({
get: function (path) {
request(this, 'GET', path);
}
});

function request(element, type, path) {
var dees = $(element);
$.ajax({
type: type,
url: '/request/'+path,
success: function(data) {
console.log('Success');

a = $(element);
b = $('#fileList'); // this is a control

dees.html(data);
}
});
}

(function() {
console.log('running');
$('#fileList').get('car/ford');
})();

我遇到的问题是当我运行 a.html(data);
一切都不会改变。但是如果我运行 b.html(data);一切都按其应有的方式进行。

所以这两个选择器之间是有区别的。
在 a 上找不到元素 a.length == 0
在 b 上找到元素 b.length == 1

为什么选择器找不到该元素以及如何修复它?

最佳答案

通过在调用函数前面添加 $ 解决了问题。

来自:

(function() {
console.log('running');
$('#fileList').get('car/ford');
})();

致:

$(function() {
console.log('running');
$('#fileList').get('car/ford');
});

关于javascript - 自定义 jQuery 函数 - 选择器找不到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12546526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com