简体   繁体   English

简单的javascript函数在IE7中不起作用

[英]Simple javascript function not working in IE7

Code works across all major browsers, but firing a simple alert on click is not working. 代码可在所有主要浏览器上使用,但是在点击时发出简单的警报无法正常工作。

This is in my header 这是在我的标题中

<script type="text/javascript">
    function this_function() {
        alert("got here mango!");
    }
</script>

This is in the body 这在体内

<button type="button" onclick="this_function()">click me</button>

If I put the "onclick" into the tag then it works fine and dandy. 如果我将“ onclick”放入代码中,则它可以正常工作。

Any and all suggestions on how to get this to work in IE would be great. 关于如何使其在IE中工作的任何建议都将是很棒的。 Thanks in advance. 提前致谢。

Sorry, by "into the tag" i meant putting onclick="alert()" into the tag. 抱歉,“放入标签”是指将onclick =“ alert()”放入标签。

Try: <button type="button" onclick="javascript:this_function();">click me</button> 尝试: <button type="button" onclick="javascript:this_function();">click me</button>

It's advised to separate JavaScript and markup. 建议将JavaScript和标记分开。 Thus regardless you should assign an ID to the button and attach the onclick handler like this: 因此,无论您应该为按钮分配一个ID并附加如下所示的onclick处理程序:

document.getElementById("button").onclick = function() {
     alert("got here mango!");
};
​

Are you running this sandboxed? 您正在运行此沙盒吗? If you aren't I would highly suggest trying this all by its self in a single HTML file with no other things going on. 如果不是这样,我强烈建议您在一个HTML文件中自行尝试全部操作,而无需进行其他操作。 It is possible that IE7 is blowing up (quietly) on another script issue that is preventing your "this_function" from loading into the DOM properly. IE7可能(静静地)在另一个脚本问题上崩溃,导致您的“ this_function”无法正确加载到DOM中。

After you have done this put the in your there is no need for it to be in the head and I have actually seen this cause problems under certain conditions 完成此操作后,就不必再将其放在头上了,而我实际上已经看到在某些情况下会导致问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM