简体   繁体   English

无法获得可在JSFiddle上运行的代码以脱机工作

[英]Cannot get code which works on JSFiddle to work offline

I have found a JS Fiddle post ( http://jsfiddle.net/luk3a/4cj7a5zx/ ) which codes almost exactly what I am trying to do. 我发现了JS Fiddle帖子( http://jsfiddle.net/luk3a/4cj7a5zx/ ),其代码几乎完全符合我的意图。 I have copied the code for my offline practices where I run the code locally in Safari, I have never used JQuery and can't get the code to function outside of the JS Fiddle environment. 我已经复制了我的离线实践的代码,其中我在Safari中本地运行代码,但是我从未使用过JQuery,并且无法使代码在JS Fiddle环境之外运行。 The aim is to take an input and compare it to an array which then says true or false as to whether the input appears in the array. 目的是获取一个输入并将其与一个数组进行比较,然后该数组对输入是否出现在数组中说是对还是错。 The input box appears in safari but nothing happens when I click 'check'. 输入框以野生动物园形式出现,但是当我单击“检查”时什么也没有发生。

I have tried putting the script in a separate node.js file, also I have tried different CDN options for different versions of JQ as well as downloading the whole package to my machine and linking it with a file path. 我尝试将脚本放在单独的node.js文件中,也尝试过针对不同版本的JQ使用不同的CDN选项,以及将整个程序包下载到我的机器上,并将其与文件路径链接。

<!DOCTYPE html>
<html>
<head>

  <script
    type="text/javascript"
    src="//code.jquery.com/jquery-1.7.1.js"

  ></script>

  <script type="text/javascript">

$(window).load(function(){

$(document).ready(function() {
  var names = ['vadim', 'thomas', 'tanya', 'timur', 'eve', 'kate', 'karen', 
'peter'];

  $('#check').click(function() {
    var name = $('#name').val();
    if (jQuery.inArray(name, names) != '-1') {
      alert(name + ' is in the array!');
    } else {
      alert(name + ' is NOT in the array...');
    }
  });
});

  </script>

</head>
<body>
    <input id="name" type="text">
<input id="check" type="button" value="Check">

  </script>
</body>
</html>

Nothing happens when I press 'Check'. 当我按“检查”时,什么也没有发生。 I am not a coder I simply want to implement this in a Wix site for a simple project. 我不是编码人员,我只是想在Wix站点中为一个简单项目实现此功能。

Try this. 尝试这个。 I have removed this line $(window).load(function(){ . You have forgot to close the braces 我已经删除了这一行$(window).load(function(){ 。您忘记了关闭花括号

<!DOCTYPE html>
<html>
<head>
 </head>

<body>
    <input id="name" type="text">
    <input id="check" type="button" value="Check">
 <script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
<script type="text/javascript">


$(document).ready(function() {
  var names = ['vadim', 'thomas', 'tanya', 'timur', 'eve', 'kate', 'karen', 
'peter'];

  $('#check').click(function() {
    var name = $('#name').val();
    if (jQuery.inArray(name, names) != '-1') {
      alert(name + ' is in the array!');
    } else {
      alert(name + ' is NOT in the array...');
    }
  });
});

  </script>

</body>
</html>

you have forget to close: 您忘记关闭:

$(window).load(function(){
   //your code
});

For your Informations: 供您参考:

$(document).ready(function(e) { 
    // executes when HTML-Document is loaded and DOM is ready  
    console.log("page is loading now"); 
});

$(document).load(function(e) { 
    //when html page complete loaded
    console.log("completely loaded"); 
});

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

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