<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaScript &#8211; Webサイト制作に関するノウハウブログ｜IWACODE</title>
	<atom:link href="https://iwacode-web.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://iwacode-web.com</link>
	<description>初心者でもわかりやすいようにWeb制作に関するノウハウを発信していきます。コーディングだけではなく、デザイン、WordPressに関する情報、フリーランスとしてのマインド、活動についても発信していきます。</description>
	<lastBuildDate>Thu, 16 Oct 2025 04:31:34 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://iwacode-web.com/wp-content/uploads/2023/12/cropped-site-icon-32x32.png</url>
	<title>JavaScript &#8211; Webサイト制作に関するノウハウブログ｜IWACODE</title>
	<link>https://iwacode-web.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/>
<atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/>
<atom:link rel="hub" href="https://websubhub.com/hub"/>
<atom:link rel="self" href="https://iwacode-web.com/tag/javascript/feed/"/>
	<item>
		<title>【JavaScript】現在のページに対応するナビゲーションメニューにスタイルを追加する方法</title>
		<link>https://iwacode-web.com/style-current-page/</link>
		
		<dc:creator><![CDATA[岩村　彰斗]]></dc:creator>
		<pubDate>Mon, 29 Jul 2024 05:42:10 +0000</pubDate>
				<category><![CDATA[HTML/CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web制作]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[コーディング]]></category>
		<guid isPermaLink="false">https://iwacode-web.com/?p=947</guid>

					<description><![CDATA[<p><img src="https://iwacode-web.com/wp-content/uploads/2024/07/blog_20240729.jpg" class="webfeedsFeaturedVisual" /></p>皆さんこんにちは！IWACODEの岩村です！ 今回は現在のページに対応するナビゲーションメニューのリンクに対して、「このリンクが今のページですよ～」といった感じでスタイルを適用する方法を解説いたします。 実際によくあるヘ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://iwacode-web.com/wp-content/uploads/2024/07/blog_20240729.jpg" class="webfeedsFeaturedVisual" /></p>
<p class="wp-block-paragraph">皆さんこんにちは！IWACODEの岩村です！</p>



<p class="wp-block-paragraph">今回は現在のページに対応するナビゲーションメニューのリンクに対して、「このリンクが今のページですよ～」といった感じでスタイルを適用する方法を解説いたします。</p>



<p class="wp-block-paragraph">実際によくあるヘッダーデザインを用いて解説していきます！</p>



<p class="wp-block-paragraph">今回は以下のようなヘッダーを作成します。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1000" height="133" src="https://iwacode-web.com/wp-content/uploads/2024/07/image-21.png" alt="" class="wp-image-951" srcset="https://iwacode-web.com/wp-content/uploads/2024/07/image-21.png 1000w, https://iwacode-web.com/wp-content/uploads/2024/07/image-21-300x40.png 300w, https://iwacode-web.com/wp-content/uploads/2024/07/image-21-768x102.png 768w" sizes="(max-width: 1000px) 100vw, 1000px" /></figure>



<p class="wp-block-paragraph">トップページを開いている想定なので、TOPの文字がオレンジになっています。</p>



<p class="wp-block-paragraph">WORKSを開いたらWORKSの文字がオレンジに・・・といったような制御を行います。</p>



<h2 class="wp-block-heading">HTML/CSS/JavaScriptの例</h2>



<h3 class="wp-block-heading">HTML</h3>



<p class="wp-block-paragraph">今回使用するデザインのHTMLを構築していきます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-html" data-lang="HTML"><code>&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;ja&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;title&gt;ナビゲーションメニューのスタイル&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
  &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
  &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
  &lt;link
    href=&quot;https://fonts.googleapis.com/css2?family=Caveat:wght@400..700&family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap&quot;
    rel=&quot;stylesheet&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;header class=&quot;header&quot;&gt;
    &lt;h1 class=&quot;header__logo&quot;&gt;iwacode&lt;/h1&gt;
    &lt;nav class=&quot;header__nav&quot;&gt;
      &lt;ul class=&quot;header__nav-list&quot;&gt;
        &lt;li class=&quot;header__nav-item&quot;&gt;&lt;a href=&quot;index.html&quot;&gt;toppage&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;header__nav-item&quot;&gt;&lt;a href=&quot;about.html&quot;&gt;works&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;header__nav-item&quot;&gt;&lt;a href=&quot;services.html&quot;&gt;srevice&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;header__nav-item&quot;&gt;&lt;a href=&quot;contact.html&quot;&gt;contact&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/nav&gt;
  &lt;/header&gt;
  &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;</code></pre></div>



<h3 class="wp-block-heading">CSS</h3>



<p class="wp-block-paragraph">一番下に<span class="swl-marker mark_yellow">JavaScriptでクラスが付与された際のスタイル</span>を記載しています。<br>※<strong><span class="swl-marker mark_yellow">.is-active</span></strong>が付与された際の指定</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-css" data-lang="CSS"><code>/* スタイルの初期化 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  list-style-type: none;
  text-decoration: none;
}

/* サイトの基本設定 */
html {
  color: #465572;
  font-size: 16px;
  letter-spacing: 0.03em;
}

.header {
  align-items: center;
  background-color: #E9ECEF;
  display: flex;
  height: 80px;
  justify-content: space-between;
  padding: 0 40px;
}

.header__logo {
  font-family: &quot;Caveat&quot;, cursive;
  font-size: 2rem;
  font-weight: bold;
}

.header__nav {
  font-family: &quot;Roboto&quot;, sans-serif;
  font-size: .925rem;
}

.header__nav-list {
  display: flex;
  gap: 2em;
}

.header__nav-item&gt;a {
  color: #465572;
  font-weight: bold;
  display: block;
  transition: .3s;
}

.header__nav-item&gt;a:hover {
  color: #FF983A;
}

/* ↓↓↓↓↓　今回のポイント　↓↓↓↓↓ */
/* JavaScriptにて.is-activeクラスが付与された場合のスタイル */
.header__nav-item&gt;a.is-active {
  color: #FF983A;
}
/* ↑↑↑↑↑　今回のポイント　↑↑↑↑↑ */</code></pre></div>



<h3 class="wp-block-heading">JavaScriptの記載</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-js" data-lang="JavaScript"><code>const currentPath = window.location.pathname;
const navLinks = document.querySelectorAll(&quot;.header__nav-item a&quot;);

navLinks.forEach(link =&gt; {
  if (link.href === currentPath) {
    link.classList.add(&quot;is-active&quot;);
  }
});</code></pre></div>



<h4 class="wp-block-heading">JavaScriptの解説</h4>



<p class="wp-block-paragraph">分かりやすいよう１行ずつコメントを入れました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-js" data-lang="JavaScript"><code>// １）定数currentPathに現在開いているページのURLを格納
const currentPath = window.location.pathname;

// ２）定数navLinksに.header__nav-item a に該当する要素を格納
// 　　複数取得できるので配列となる
const navLinks = document.querySelectorAll(&quot;.header__nav-item a&quot;);

// ３）２で取得した要素をループで処理する
navLinks.forEach(link =&gt; {

  // ４）ここでのlink.hrefはaタグのhref属性に指定しているURL
  // 　　currentPathには現在開いているページのURLが入っているので、
  // 　　if文の===で比較し、一致している要素の場合、中の処理に入る
  if (link.href === currentPath) {

    // ５）linkにis-activeクラスを付与
    // 　　※linkはaタグのこと
    link.classList.add(&quot;is-active&quot;);
  }
});</code></pre></div>



<p class="wp-block-paragraph">処理の概要としては、aタグで指定しているURLと現在開いているページのURLを比較して、同一であれば、一致するhref属性を持つaタグに対して.is-activeクラスを付与するといったものになります。</p>



<h2 class="wp-block-heading">さいごに</h2>



<p class="wp-block-paragraph">いかがでしたでしょうか。</p>



<p class="wp-block-paragraph">上記の処理を行うことで、現在開いているページが閲覧している方も一目でわかるため、ページの回遊性にも繋がります。</p>



<p class="wp-block-paragraph">比較的簡単なJavaScriptの処理で実装可能なため、仕様を理解しぜひ組み込んでいってください！</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
