javascript onclick not working in jsfiddle

前端 未结 5 1924
既然无缘
既然无缘 2020-12-17 15:34

I am trying to make jsfiddle , my onclick is not working in jsfiddle. what is wrong in my code



        
5条回答
  •  余生分开走
    2020-12-17 15:51

    As others said, for first case you have to set No wrap - in or No wrap - in as javascript panel settings (the blue link at js panel top-right).

    For the second (your Edit) question, your code inside a function and the js will run it (within a new context), but it will do nothing as you just define a function and ignore it without any call or assignment.

    if you call alert(myclick) you will see all the code is executed and its defined at that point. see this fiddle

    $(document).ready(function() {
    
      //alert("ready is executed");
    
      function myclick(){
        alert("myclick is called")
        window.location.reload(true);
      }
    
      alert(myclick); //this will show the myclick is a defined function!
    
      //document.getElementsByTagName("input")[0].onclick = myclick;
    })
    

    if you call this:

    document.getElementsByTagName("input")[0].onclick = myclick;
    

    in that $(document).ready({...}) scope, it will be assigned to the button and works as you wish.

提交回复
热议问题