2012-05-22 34 views
0

所以我有一个站点URL字符串循环的例子,并试图添加页面到链接,但我不想处理该域名,因为它是固定的。现在它不起作用..但我试图修复它。网站地图JQuery风格..使用attr。加页网址

<script> 
    $(document).ready(function() { 

    url = $(this).attr('href', $(this).attr('href')); 

     $('#sitemap a').attr('href','http://site.com/?c=123&p='+url); 

    }); 
    </script> 

    // trying to get output http://site.com/?c=123&p=page1.html 
    <div id="sitemap"> 
    <a href="page1.html">test</a> 
    <a href="page2.html">test</a> 
    <a href="page3.html">test</a> 
    </div> 
+0

_ How does not work it?解释你有什么问题,并且你可能会得到答案。 – Bojangles

+0

我正在尝试获取#sitemap – Blynn

回答

1

所以你婉做什么居然是:

<script> 
    $(document).ready(function() { 
     $('#sitemap a').each(function() { 
      // Cache the jQuery object 
      var current = $(this); 

      // Get the current url 
      var currentUrl = current.attr('href'); 

      // Replace the current url with the new one (appending the url above) 
      current.attr('href', 'http://site.com/?c=123&p=' + currentUrl); 
     }); 
    }); 
</script> 

在这一行的,你写的代码,this实际上是指document。并且document没有称为href的属性。

url = $(this).attr('href', $(this).attr('href')); 
+0

内部链接http://site.com/?c=123&p=page1.html的输出感谢您的帮助! – Blynn

+0

没问题!尽管现在我确实使它更具可读性,但=) –