简体   繁体   English

javascript警报功能奇怪的行为

[英]javascript alert function strange behaviour

So I am learning JavaScript.... I have gone way past this basic stuff in my learning but can't figure out why this wont work: 所以我正在学习JavaScript ....我在学习中已经超越了这些基本的东西,但无法弄清楚为什么这不会起作用:

   function message(){
      alert("message"); 
    }

<button onclick="message()">Click me</button>

On Js Bin all is ok http://jsbin.com/uwutut/1/edit 在Js Bin上一切都很好http://jsbin.com/uwutut/1/edit

On Js fiddle the alert is not triggered!! 在Js小提琴上没有触发警报! http://jsfiddle.net/vincentieo/D2dxA/ http://jsfiddle.net/vincentieo/D2dxA/

It is the exact same code...copy and paste so no reason why js fiddle is being fussy?? 这是完全相同的代码...复制和粘贴所以没有理由为什么js小提琴是挑剔?

The jsfiddle infrastructure puts your code in an event handler for the "load" event if you don't tell it not to. 如果您不告诉它,jsfiddle基础结构会将您的代码放入“load”事件的事件处理程序中。 That means that your function is a local function to that handler, so it's not visible globally. 这意味着您的函数是该处理程序的本地函数,因此它不是全局可见的。

On the left side of the jsfiddle UI, look for a pull-down about where the code goes, and choose "no-wrap (body)". 在jsfiddle UI的左侧,查找代码所在位置的下拉菜单,然后选择“no-wrap(body)”。 See if that helps. 看看是否有帮助。

it works perfectly if you move your javascript to your header 如果你将你的javascript移动到你的标题,它的工作完美

http://jsfiddle.net/D2dxA/2/ http://jsfiddle.net/D2dxA/2/

<head>
<script>
function message(){
  alert("message"); 
}
</script>
</head>

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

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