简体   繁体   English

在 Sublime Text 中运行 JavaScript

[英]Run JavaScript in Sublime Text

I have searched and found a way to make JavaScript works on SublimeText.我已经搜索并找到了一种使 JavaScript 在 SublimeText 上工作的方法。 The way is like that in steps:方法是这样的:

  • Sublime Text > File > New File > Type: console.log("Hello World"); Sublime Text > 文件 > 新建文件 > 类型:console.log("Hello World");
  • Press Ctrl + S (Save) > script.js按 Ctrl + S(保存)> script.js
  • Install Node.js安装 Node.js

  • Sublime Text > Tools > Build System > New Build System > Type: Sublime Text > 工具 > 构建系统 > 新构建系统 > 类型:

 { "cmd": ["node", "$file"], "selector": "source.js" }

  • Save As Node.sublime-build另存为 Node.sublime-build
  • Sublime Text > Tools > Build System > Node Sublime Text > 工具 > 构建系统 > 节点

That worked well and I can get the console results well Now how can I use or refer to the index.html file?效果很好,我可以很好地获得控制台结果现在我如何使用或参考 index.html 文件? To be more specific I have two files: index.html and script.js and I need to run the JS on sublime text but I got ReferenceError: document is not defined on such a line更具体地说,我有两个文件:index.html 和 script.js,我需要在崇高的文本上运行 JS,但我得到ReferenceError: document is not defined on such line

 var output = document.getElementById('output');

document object .文档 object relates to the DOM(Document Object Model) in the Web Browsers.涉及 Web 浏览器中的 DOM(文档 Object 模型)。 Node.js is desktop JavaScript runtime environment, It'll not give to access to DOM objects. Node.js是桌面JavaScript运行环境,它不会授予对 DOM 对象的访问权限。

If you want to access the document, object in Node.js you can use browserify如果要访问Node.js中的object文档可以使用browserify

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

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