gpt4 book ai didi

javascript - $ is not defined 错误在 mootools

转载 作者:行者123 更新时间:2023-11-29 21:46:56 25 4
gpt4 key购买 nike

(function () {
//alert("hi");
var divtag = $('maindiv').get('tag');
//var divtag=document.getElementById("maindiv");
console.log(divtag);

var username = new Element("input", {
"type": "text",
"name": "username",
"id": "name",
"placeholder": "Enter username here"
});
divtag.inject(username);
})();

这是我的 js 文件的代码。请让我知道我哪里出错了。

最佳答案

如果可行:

(function () {

// >>> EXPLICITLY SET $ TO document.id:
var $ = document.id;

// >>> THE REST OF YOUR CODE:

//alert("hi");
var divtag = $('maindiv').get('tag');
//var divtag=document.getElementById("maindiv");
console.log(divtag);

var username = new Element("input", {
"type": "text",
"name": "username",
"id": "name",
"placeholder": "Enter username here"
});
divtag.inject(username);
})();

然后 document.id$ 别名未定义,或者由 MooTools 以外的其他库定义(也许 jQuery 也包含在页面中?)。

(将 $ 重新定义为 var $ = document.id; 在您自己的函数中是安全的,即使 $ 已被定义为在你的函数之外使用的其他东西 - 外面的 $ 不会在这里改变。)

另一件事是这样的:

var divtag = $('maindiv').get('tag');

这可能只是:

var divtag = $('maindiv');

我假设您已在页面上加载了 MooTools 库 - 如果您不这样做,那么当然没有任何效果。

在 JSFiddle、JS Bin 或 CodePen 上发布指向代码示例的链接将使诊断问题变得更加容易。

关于javascript - $ is not defined 错误在 mootools,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30800069/

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