jquery $.each or javascript for loop

跟風遠走 提交于 2019-12-11 03:49:02

问题


i'm a javascript, jquery newbie :) how can i make a loop here on all the href within #rfr-topnav using javascript or jquery? thanks in advance.

    <script type="text/javascript">
$(document).ready(function(){
        var sel="#rfr-topnav a[href*='#root#']";
        var href=$(sel).attr('href');
        var rootUrl = $('#ctl00_RootUrlId').attr('value');
        var newhref=rootUrl+href.substr(href.indexOf('#root#')+6);
        $(sel).attr ('href',newhref);
});
</script>

回答1:


Refactoring slightly, I think this is what you want:

$(document).ready(function(){
    var $sel = $("#rfr-topnav a[href*='#root#']");
    var rootUrl = $('#ctl00_RootUrlId').val();

    $sel.each(function() {
        var $this = $(this), href = $this.attr('href');
        $this.attr('href', rootUrl + href.slice(href.indexOf('#root#') + 6));
    });
});


来源:https://stackoverflow.com/questions/4656973/jquery-each-or-javascript-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!