全ページのcopyrightの上にfooter用のナビゲーションを追加したいです。
全ページに入れたいのですが、Object.keysでキーと値をlistのliタグ内で展開したいのですがPHPのように組み込みがうまくいかなく。
レクチャー頂ければ幸いです。
※querySelectorでcopyrightを取得してそちらの上部にHTMLを差し込みたいです
var obj = { テキストa:"localhost/index", テキストb:"localhost/index2", テキストc:"localhost/index3", }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); console.log(HtmlDoc());
document.addEventListener('DOMContentLoaded', function() { const targetElement = document.querySelector('.copyright'); const HtmlDoc = ` <div class="footer"> <ul class="footer_navlist"> <li><a href="localhost/index">テキストa</a></li> <li><a href="localhost/index2">テキストb</a></li> <li><a href="localhost/index3">テキストb</a></li> <li></li> <li></li> <li></li> <li></li> </ul> <div> `; targetElement.insertAdjacentHTML('beforebegin', HtmlDoc); });
PHPで恐縮ですが再現したいのは以下のような物です
<?php $navlist = [ 'index' => 'テキストa', 'index2' => '"テキストb', ]; ?> <li> <?php foreach($navlist as $key => $text):?> <li><a href="<?php echo $key ?>"><?php echo $text ?> </a> </li> <?php endforeach; ?> </li>
回答2件
あなたの回答
tips
プレビュー