sách gpt4 ăn đã đi

javascript - 内联 javascript 函数调用 - 元素列表后缺少 ]

In lại Tác giả: Walker 123 更新时间:2023-11-29 17:19:45 25 4
mua khóa gpt4 giày nike

我有一个代码:

var data = $('Element information');

其中元素是一个对象。

此外,我还定义了一个函数:

someFunction: function(element) {
console.log(element);
.... // some code
}

但是当 span 元素尝试调用该函数时,出现错误:

SyntaxError: missing ] after element list 
someFunction([object Object]);

当我在 Firebug 中调试它时,我看到了


Element information

我通常如何使用具体元素作为参数来调用该函数?

câu trả lời hay nhất

您将无法传递该元素,因为它在您的连接中被转换为字符串。当一个对象被转换为字符串时,它输出:[Object object]。这就是您在调试中看到的。

我的建议:您可以将元素作为数据添加到跨度中,例如:

$('span).data('element', element);

然后在 someFunction 中像这样检索它:

var element = $(this).data('element');

另一种选择是在初始化元素的位置绑定(bind)到 Javascript 中的单击。像这样

function anotherFunction() {
var element = {};

// Initialize element
...

// I have already got the element initialized, now I can bind the click
$('span').click(function() {
// For your debug and validation
console.log(JSON.stringify(element));
});
}

关于javascript - 内联 javascript 函数调用 - 元素列表后缺少 ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13834257/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress