JavaScript OnClick 返回错误对象不是函数
我有一个函数,当单击一个元素时,它会使该元素成为对象中的选定元素。让我告诉你。
var elem = new Object();
elem = {
title:'',//string
num:null,//number
selected:null
}
function selected(elem){
elem.title = elem.getAttribute("title") || this['title'];
alert(elem.title);
for(x=0;x<classIds.length;x++){
if(elem.title==classIds[x].name){
elem.num=x;
elem.selected=classIds[x];
alert(elem.selected.properties);
}
}
}
因此,当单击某个元素时,所选函数将从该元素上的 onlick attr 运行。这很酷。效果很好。但是,如果再次单击它,浏览器会给出错误“对象不是函数”。仅当您连续单击同一元素时才会发生这种情况。如果您单击另一个元素,则不会发生这种情况。这很奇怪,因为该函数应该单独运行并覆盖对象 elem (它在函数外部定义为全局变量/对象)。由于调试原因,我收到了警报。此外,数组 classIds 也是在函数之外定义的。任何见解都会很棒。另外,我知道我的编码有点奇怪,我真的只是从 JavaScript 中的对象和方法开始。
编辑
Onclick 是这样调用的
<li title="+classIds[x].name+" onclick='selected(this)'>"+classIds[x].name+"</li>
所以...
onclick='selected(this)'
是调用
I have a function where when an element is clicked, it makes that element the selected element in a an Object. Let me show you.
var elem = new Object();
elem = {
title:'',//string
num:null,//number
selected:null
}
function selected(elem){
elem.title = elem.getAttribute("title") || this['title'];
alert(elem.title);
for(x=0;x<classIds.length;x++){
if(elem.title==classIds[x].name){
elem.num=x;
elem.selected=classIds[x];
alert(elem.selected.properties);
}
}
}
So when an element is clicked, the selected function runs from an onlick attr on the element. Which is cool. It works fine. But, if you click on it again, the browser gives the error Object is not a function. This only happens when you click on the same element consecutively. If you click on another element, it doesn't happen. Which is weird because the function should be running a seperate time and overwriting the Object elem (which is defined outside the function as a global variable/object). I have the alert for reasons of debugging. Also, the array classIds is defined out of the function as well. Any insight would be great. Also, I know my coding is a little odd, I am really just starting with Objects and Methods in JavaScript.
Edit
Onclick is called like this below
<li title="+classIds[x].name+" onclick='selected(this)'>"+classIds[x].name+"</li>
So...
onclick='selected(this)'
Is the the call
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有一个非常明显的问题:您声明了一个名为
elem
的全局变量(在函数外部),然后您的函数有一个也称为elem
的参数。因此,函数内对 elem 的每次引用都将指向参数,而不是全局参数。根据您调用该函数的方式,该参数引用您的元素,这意味着该函数当前正在覆盖该元素和/或在该元素上创建属性。
如果您更改参数的名称,例如
clickedElem
,那么在函数中,当您指的是全局变量时可以使用elem
,而当您指的是全局变量时可以使用clickedElem
你的意思是参数。除此之外,我不太确定你想要实现什么,所以我不知道还有什么建议。
(顺便说一句,正如我在上面的评论中所说,初始化
elem = new Object()
是没有意义的,因为在下一行中,您立即将其指定为等于其他内容。但这不是会给你带来麻烦,毫无意义。)There's one really obvious problem in your code: you're declaring a global variable (outside your function) called
elem
, and then your function has a parameter also calledelem
. So every reference toelem
within the function will be to the parameter not to the global. Given how you're calling the function the parameter refers to your<li>
element so that means the function is currently overwriting and/or creating properties on that element.If you change the name of the parameter, to say
clickedElem
then within the function you can useelem
when you mean the global variable andclickedElem
when you mean the parameter.Beyond that I'm not quite sure what you're trying to achieve so I don't know what else to advise.
(And as an aside, as I said in a comment above, there's no point initialising
elem = new Object()
because on the next line you immediately assign it equal to something else. But that isn't going to cause you a problem, it's just pointless.)