我正在尝试使用香草JS(无jQuery等)将参数附加到菜单中多个项目的URL上。
我要使用的所有菜单项都有一个共同点,我认为这是选择正确对象的关键:它们都以相同的ID开头。
所以我看起来像这样:
<a href="someplace.php" id="actionMenu123">Link 1</a>
<a href="someplace2.php" id="actionMenu456">Link 2</a>
<a href="someplace3.php" id="actionMenu789">Link 3</a>
<a href="someplace4.php" id="actionMenuABV">Link 4</a>
<a href="someplace5.php" id="actionMenu5X4">Link 5</a>
我需要的JS脚本应产生以下结果:
<a href="someplace.php?John=Doe" id="actionMenu123">Link 1</a>
<a href="someplace2.php?John=Doe" id="actionMenu456">Link 2</a>
<a href="someplace3.php?John=Doe" id="actionMenu789">Link 3</a>
<a href="someplace4.php?John=Doe" id="actionMenuABV">Link 4</a>
<a href="someplace5.php?John=Doe" id="actionMenu5X4">Link 5</a>
我试图<a>
通过使用querySelectorAll选择所有DOM中包含“ actionMenu”的对象来重写href,但是我认为我做的不正确。
我将不胜感激任何指针。
// first, get list of all 'a' tags and convert to array
[].slice.call(document.querySelectorAll('a'))
// filter for ones whose id matches. We also want to make
// sure it *has* an id and an href
.filter(node => node.id && node.href && node.id.match(/^actionmenu/i))
// rewrite the href
.forEach(node => node.href = node.href + '?John=Doe');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句