- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试根据我的应用程序中当前处于事件状态的选项卡来运行 AJAX 函数。当我在某些事件后调用该函数时,一切正常,但我无法使用字符串变量动态调用该函数。
遵循这个答案:https://stackoverflow.com/a/359910/5950111我刚刚收到一个 TypeError,描述我调用的对象不是一个函数。
这是我的ajax函数:
function home_tab_fetchMore(items_count) {
let request = new XMLHttpRequest();
let output = []
request.open('GET', `feedstream/${items_count}`);
request.onload = function () {
if (request.status === 200 && request.responseText != '') {
let new_items = JSON.parse(request.responseText);
for (let i = 0; i < new_items.length; i++) {
let item = new_items[i];
let event_id = feed_stream_row.children.length + 1;
let feed_item = htmlToElement(`
<div class="grid-item" id="eventcard_${event_id}">
<div class="card-header">Featured</div>
<div class="card-body">
<h5 class="card-title">${item.fields['title']}</h5>
<p class="card-text">${item.fields['appointment']}
<br>
${dummy_text.sentence(5, 40)}
</p>
</div>
</div>
`);
// appending new item to DOM and updating masonry laytout
feed_stream_row.appendChild(feed_item);
msnry.appended(feed_item);
output.push(feed_item);
}
} else {
console.log('no response!');
}
};
request.send();
return output;
};
这是调用者事件:
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() >= $(document).height()) {
var fnstring = `${current_active_tab().id}_fetchMore`;
var fn = window[fnstring]
console.log(typeof(fnstring)) // returns: string type
console.log(fnstring) // returns: home_tab_fetchMore without quotes
console.log(fn) // returns: undefined
fn(2) // returns: Uncaught TypeError: fn is not a function
// however, this line works just as expected:
home_tab_fetchMore(2)
};
});
TypeError 中还有一些我无法理解的行:
Uncaught TypeError: fn is not a function
at main.js:128
at dispatch (jquery-3.3.1.slim.min.js:2)
at v.handle (jquery-3.3.1.slim.min.js:2)
感谢您提前提出的任何建议和指导,谢谢。
最佳答案
function home_tab_fetchMore(items_count) {}
除非调用,否则上述函数定义在 window
对象中不可用。相反,使用函数表达式(在定义函数表达式之前不能使用它们)。
home_tab_fetchMore = function() {} OR
window.home_tab_fetchMore = function() {}
关于JavaScript 窗口 ["functionName"](参数) 返回 "is not a function"TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53458116/
当前 Google 涂鸦的标记如下所示: 有趣的部分是 onload="window.lol&&lol()" 出于好奇:window.functionname&&functionname() 到底做
我注意到您可以使用或不使用括号来引用函数。为什么?有什么区别? 顺便说一句,我注意到这是有效的:window.onload = functionName; 而这不是:window.onload = f
jQuery 函数有什么区别 $.FunctionName = function(){ alert('Hello!') } 和正常的 javascript 函数? function FunctionN
我知道你可以直接使用名称来获取变量和调用函数 variable.functionName 或使用名称作为字符串 variable["functionName"] or variable[functio
在 jQuery 中,我见过以下两种定义 jQuery 函数的方法: $.fn.CustomAlert = function() { alert('boo!'); }; $.CustomAlert
我正在研究 angularJs 并致力于 javascript 优化。我最近看了YUI创建者的一个视频,他是一位JavaScript专家,关于JavaScript优化。他解释了变量声明和作用域以及 J
const heightOutput = document.querySelector('#height'); const widthOutput = document.querySelector('
我研究 javascript 中的函数继承。根据我读过的文章,我写了代码: function Base(){ var enableVar = true this.enable = func
我有 window.getTask = getTask window.postTask = postTask window.logIn = logIn 我正在寻找一种简写方式来做到这一点。类似 win
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Do you ever need to specify javascript: in an onclick?
我已经使用 ReactJs 几天了。我发现有些语法有点奇怪。例如,有时我必须这样调用函数: {this.functionName} 末尾没有括号。 有时我必须这样调用它: {this.function
我已经使用 ReactJs 几天了。我发现有些语法有点奇怪。例如,有时我必须这样调用函数: {this.functionName} 末尾没有括号。 有时我必须这样调用它: {this.function
这个关闭代码可以工作: function setupSomeGlobals(){ var num = 666; gPrintNumber = function() { // play
我最近开始维护别人的 JavaScript 代码。我正在修复错误、添加功能并尝试整理代码并使其更加一致。 之前的开发人员使用了两种声明函数的方式,我无法确定其背后是否有原因。 这两种方式是: var
我最近开始维护别人的 JavaScript 代码。我正在修复错误、添加功能并尝试整理代码并使其更加一致。 之前的开发人员使用了两种声明函数的方式,我无法确定其背后是否有原因。 这两种方式是: var
我最近开始维护别人的 JavaScript 代码。我正在修复错误、添加功能并尝试整理代码并使其更加一致。 之前的开发人员使用了两种声明函数的方式,我无法确定其背后是否有原因。 这两种方式是: var
我最近开始维护别人的 JavaScript 代码。我正在修复错误、添加功能并尝试整理代码并使其更加一致。 之前的开发人员使用了两种声明函数的方式,我无法确定其背后是否有原因。 这两种方式是: var
我最近开始维护别人的 JavaScript 代码。我正在修复错误、添加功能并尝试整理代码并使其更加一致。 之前的开发人员使用了两种声明函数的方式,我无法确定其背后是否有原因。 这两种方式是: var
我最近开始维护别人的 JavaScript 代码。我正在修复错误、添加功能并尝试整理代码并使其更加一致。 之前的开发人员使用了两种声明函数的方式,我无法确定其背后是否有原因。 这两种方式是: var
我最近开始维护别人的 JavaScript 代码。我正在修复错误、添加功能并尝试整理代码并使其更加一致。 之前的开发人员使用了两种声明函数的方式,我无法确定其背后是否有原因。 这两种方式是: var
我是一名优秀的程序员,十分优秀!