JavaScript
// 質問と選択肢のつながり。next は次の質問(q と choices)か、答え(title と text)
const fq5Tree = {
q: '何についてのご質問ですか?',
choices: [
{ label: '注文した商品のこと', next: {
q: '商品はもう届いていますか?',
choices: [
{ label: 'まだ届いていない', next: { title: '届くまでの日数と、配送状況の調べ方', text: '発送から1〜3日でお届けします(離島は5日ほど)。発送のときにお送りするメールの追跡番号から、いまの配送状況を調べられます。5日たっても届かないときは、お問い合わせください。' } },
{ label: '届いたが、困っている', next: {
q: 'どんなことでお困りですか?',
choices: [
{ label: '壊れていた・ちがう商品が届いた', next: { title: '交換の手続き', text: '届いてから7日以内に、注文番号と商品の写真をお問い合わせフォームからお送りください。送料は当店の負担で、新しい商品と取り替えます。' } },
{ label: 'イメージとちがった', next: { title: '返品の条件', text: '届いてから7日以内で、開けていない商品なら返品できます。返送の送料はお客さまのご負担です。マイページの「注文履歴」から申し込めます。' } },
],
} },
],
} },
{ label: 'お支払いのこと', next: {
q: '知りたいことを選んでください',
choices: [
{ label: '使える支払い方法', next: { title: '使える支払い方法', text: 'クレジットカード、コンビニ払い、銀行振込、代金引換が使えます。コンビニ払いと銀行振込は、ご注文から7日以内にお支払いください。' } },
{ label: '領収書がほしい', next: { title: '領収書の出し方', text: '発送後に、マイページの「注文履歴」から領収書(PDF)を出せます。宛名はそのときに入力できます。' } },
],
} },
{ label: 'ログイン・会員情報のこと', next: {
q: '知りたいことを選んでください',
choices: [
{ label: 'パスワードを忘れた', next: { title: 'パスワードの設定し直し', text: 'ログイン画面の「パスワードを忘れた方」から、登録したメールアドレスを入力してください。設定し直すためのメールが届きます。' } },
{ label: '退会したい', next: { title: '退会の手続き', text: 'マイページの「会員情報」のいちばん下にある「退会する」から手続きできます。退会すると、ためたポイントは消えます。' } },
],
} },
],
};
const fq5Step = document.getElementById('fq5-step');
const fq5Trail = document.getElementById('fq5-trail');
const fq5Path = []; // 選んだ選択肢の番号
const fq5El = (tag, cls, text) => {
const el = document.createElement(tag);
if (cls) el.className = cls;
if (text) el.textContent = text;
return el;
};
const fq5Render = (moveFocus) => {
// これまでに選んだことをたどる
fq5Trail.replaceChildren();
let node = fq5Tree;
fq5Path.forEach((i) => {
fq5Trail.append(fq5El('li', '', node.choices[i].label));
node = node.choices[i].next;
});
fq5Trail.hidden = fq5Path.length === 0;
fq5Step.replaceChildren();
let heading;
if (node.choices) {
heading = fq5El('h3', 'fq5-q', node.q);
const list = fq5El('ul', 'fq5-choices');
node.choices.forEach((c, i) => {
const btn = fq5El('button', 'fq5-choice', c.label);
btn.type = 'button';
btn.addEventListener('click', () => { fq5Path.push(i); fq5Render(true); });
const li = fq5El('li');
li.append(btn);
list.append(li);
});
fq5Step.append(heading, list);
} else {
heading = fq5El('h3', 'fq5-q', node.title);
const box = fq5El('div', 'fq5-answer');
const contact = fq5El('a', 'fq5-contact', '解決しないときは、お問い合わせください');
contact.href = '/contact/';
box.append(fq5El('p', 'fq5-label', '答え'), heading, fq5El('p', '', node.text), contact);
fq5Step.append(box);
}
const nav = fq5El('div', 'fq5-nav');
if (fq5Path.length) {
const back = fq5El('button', '', 'ひとつ戻る');
back.type = 'button';
back.addEventListener('click', () => { fq5Path.pop(); fq5Render(true); });
nav.append(back);
}
if (!node.choices) {
const restart = fq5El('button', '', '最初から選び直す');
restart.type = 'button';
restart.addEventListener('click', () => { fq5Path.length = 0; fq5Render(true); });
nav.append(restart);
}
if (nav.children.length) fq5Step.append(nav);
heading.tabIndex = -1;
if (moveFocus) heading.focus();
};
fq5Render(false);