2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

VSCodeを使ってVanillaのテンプレートを作成する

Last updated at Posted at 2024-01-16

はじめに

JavaScriptの基礎を理解するにあたって、まずは Vanilla を使ってかんたんなWEBページを作成してみよう!
Vanilla :ライブラリを全く使わない超シンプルなJavaScriptのこと!

問題

クラウドIDE(CodeSandboxやStack Blitz)のVanillaテンプレートを使いたかったが、
アップデートの影響で無くなってしまった。

解決方法

VSCode を使ってVanillaのテンプレートを作成する

必要なファイルはこちら!

1.index.html:
プロジェクトのメインのHTMLファイル。
ページの構造や基本的なマークアップを定義する。
CSSとJavaScriptファイルを読み込むためのリンクが存在する。

index.html
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Vanilla Project</title>
    <link rel="stylesheet" href="styles.css">
</head>

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

</html>

2.styles.css:
プロジェクトのスタイルを定義するCSSファイル。
ページの外観やスタイルを設定する。

styles.css
/* styles.css */
body {
    font-family: cursive, sans-serif;
    margin: 10;
    padding: 0;
}

3.index.js (または app.js や main.js など):
プロジェクトのメインのJavaScriptファイル。
DOMのイベントリスナーやページの初期化コードがここに書かれる。
ページが読み込まれたときに実行されるコードや、ユーザーのアクションに対する応答などが含まれる。

index.js
// index.js
document.addEventListener('DOMContentLoaded', function () {

    // "Hello, world"を表示する
    document.body.textContent = 'Hello, Vanilla!';
});

おわりに

「ファイルの中身どうやって書くんだっけ?」ってときは、VSCodeの拡張機能(Emmet)を使うとコマンド一発で作成してくれるみたいです。便利ですね!

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?