简体   繁体   English

我的Safari浏览器未执行我的JavaScript

[英]My javascript is not being executed in my safari browser

I am using Safari and the Atom text editor. 我正在使用Safari和Atom文本编辑器。 I have a basic html file with javascript code between script tags. 我有一个基本的html文件,脚本标签之间有javascript代码。 However when I click on the folder that the code is saved in none of my javascript code is working in the browser, nor my Chrome browser. 但是,当我单击代码保存的文件夹时,我的JavaScript代码都无法在浏览器中运行,也无法在Chrome浏览器中运行。 I double checked that javascript was enabled and that there were no plug-ins causing it to not run. 我再次检查了javascript是否已启用,并且没有任何插件导致其无法运行。 I am trying to make an alert if the user's value is 0 and substitute the for the value entered into the text box once the "Click Me" button is clicked. 如果用户的值为0,我想发出警报,并在单击“单击我”按钮后用替换为在文本框中输入的值。

When I use an "onload...alert" attribute in the body element , the javascript worked perfectly. 当我在body元素中使用“ onload ... alert”属性时,javascript可以正常工作。

Is it the text-editor? 是文字编辑器吗? My code? 我的密码? Any ideas? 有任何想法吗?

My code is below: 我的代码如下:

 function substitute() { var myValue = document.getElementById('myTextBox').value; if (myValue.length == 0) { alert ('Enter a Real Value'); return; } var myTitle = document.getElementById('title'); myTitle.innerHTML=myValue; } 
 <!doctype html> <html> <head> <title>Javascript Example</title> </head> <body> <h1 id="title">Javascript Example</h1> <form> <input type="text" id="myTextBox"/> <input type="submit" value="Click Me" onclick="substitute"/> </form> </body> </html> 

For starters, are you linking the JS file anywhere in your html? 首先,您是否将JS文件链接到html中的任何位置? At the end of your body tag, you should add something like this: 在body标签的末尾,您应该添加以下内容:

<script src="script.js"></script>

Also, 'substitute' is a function so it should be 'substitute()' 另外,“ substitute”是一个函数,因此应为“ substitute()”

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

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