【问题标题】:jquery $.each or javascript for loopjquery $.each 或 javascript for 循环
【发布时间】:2011-06-07 02:49:30
【问题描述】:

我是 javascript、jquery 新手 :) 我如何使用javascript或jquery在#rfr-topnav中的所有href上创建一个循环? 提前致谢。

    <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>

【问题讨论】:

  • 你能解释一下这个要求吗?

标签: javascript jquery each


【解决方案1】:

稍微重构一下,我想这就是你想要的:

$(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));
    });
});

【讨论】:

猜你喜欢
  • 2014-10-19
  • 2013-08-12
  • 2015-11-12
  • 1970-01-01
  • 2012-09-20
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 2022-08-04
相关资源
最近更新 更多