我有一系列水平 div 框,我需要添加相关的 href 以使用锚链接链接到下一个。由于它们是动态生成的,我需要使用 JavaScript 添加 href。
期望的效果将是:
<div id="post1">
<a class="next-video" href="#post2">NextVideo</a>
</div>
<div id="post2">
<a class="next-video" href="#post3">NextVideo</a>
</div>
添加了脚本
$('.next-video').each(function(index) {
$(this).attr('href', '#post' + (index + 2));
});
但似乎没有针对.next-video
类,这是现场版本:http://www.warface.co.uk/clients/detail-shoppe/test-scroll http://www.warface.co.uk/clients/detail-shoppe/test-scroll
非常感谢
你可以使用做这样的事情.attr() http://api.jquery.com/attr/:
$("a.next-video").attr('href', function(i) {
return '#post' + (i+2);
});
从 jQuery 1.4+ 开始,.attr() http://api.jquery.com/attr/采用一个函数使其非常干净(并且运行起来更便宜)。
或者,如果您不知道邮政编号(例如,它们只是不按数字顺序排列),您可以从下一个获得它<div>
, 像这样:
$("a.next-video").attr('href', function(i) {
return '#' + $(this).parent().next("div[id^='post']").attr('id');
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)