Onclick属性不会随着javascript而改变 [英] Onclick attribute wont change with javascript

查看:50
本文介绍了Onclick属性不会随着javascript而改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我点击了一个图片:

I have an image people click:

<img id="himg" src=""  önclick="menu(1);" />





我有javascript来改变onclick属性:





And i have javascript to change the "onclick" attribute:

function change(){
    var y = "menu(0)";
    return y;
}
function change1(){
    var y = "menu(1)";
    return y;
}
function menu(x){
    if(x==1){
    document.getElementById("menu").innerHTML="<h3><ul><li>Home</li><li>About</li><li>Profile</li><li>Coins</li><li>News</li></ul></h3>";
    document.getElementById("himg").onclick = change();
    }
    if(x===0){
        document.getElementById("menu").innerHTML=" ";
        document.getElementById("himg").onclick = change1();
    }
}





我不知道发生了什么,但是onclick没有改变!!!



I dont know whats happening, but the onclick doesnt change!!!

推荐答案

0)你所做的不是跨浏览器。

1)使用jquery,忘记普通的javascript:https://api.jquery.com/category/events/event-handler-attachment/ [ ^ ],更准确地说是绑定 [ ^ ]方法。

2)通过添加圆括号( = change1())你不分配函数,你调用函数并分配它的结果。但这不是你想要的。

3)只需删除括号: = change1;
0) What you do is not cross-browser.
1) Use jquery, forget plain javascript: https://api.jquery.com/category/events/event-handler-attachment/[^], more precisely the bind[^] method.
2) by adding round brackets (= change1()) you don't assign the function, you call the function and assign it's result. But this is not what you want.
3) Simply remove brackets: = change1;


In除了ZoltánZörgő在解决方案1中的好建议之外,我还会建议另外一件事。



我认为,在大多数情况下,元素处理程序的动态更改是糟糕的编程风格。你可以这样做,但它使得代码纯粹是可以支持的,特别是如果你有更多的选项而不是 x == 0 x == 1 。而且,在这种方法中,这个0和1变成了一些与它的语义无关的神奇数字,如果你正确命名带有菜单选项的变量,这将是很清楚的。



如果您使用更传统的方法,从不更改处理程序(或仅将处理程序设置为HTML元素一次),您可以获得更好,更重要的是,更可维护的结果;是的,最好使用jQuery。为此,您应该只使用一个处理程序,在其中实现当前更改 change1 的合并行为(一个更语义上的非声音命名)。在处理程序内部,您可以使用带有表示所有菜单选项的分支的if或switch块,并且对每个分支的响应的实现可以调用不同或相同的功能。



-SA
In addition to good suggestions by Zoltán Zörgő in Solution 1, I would suggest one more thing.

I think, in most cases dynamic change of the handler of the element is a bad style of programming. You can do it, but it makes the code purely supportable, especially if you have more options than x==0 and x==1. Moreover, in such approach, this 0 and 1 becomes some "magic numbers" not connected with its semantics which would be clear if you had properly named variables carrying the menu options.

You can get much better, and, more importantly, more maintainable results if you used more traditional method, never changing the handler (or setting a handler to an HTML element only once); and yes, preferably using jQuery. For this purpose, you should use only one handler, where you implement merged behavior from your current change and change1 (one more semantically non-sound naming). Inside the handler, you could have the "if" or "switch" block with branches representing all the menu choices, and implementation of the response to each branch could call different or the same functions.

—SA


这篇关于Onclick属性不会随着javascript而改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆