从URL中获取内容并将其放在容器中

问题描述:

我想使用jQuery从url中查找最后一个页面名称/目录,并在<h3>容器的页面上显示它。例如:/ _blog/PunkLogic_News/tag/videos /我想在页面上的特定<h3 class="urltag">中显示'视频'。/_blog/PunkLogic_News/tag/Noosa_Biosphere /我想显示'Noosa Biosphere'没有下划线。我想所有的特殊字符也需要删除。从URL中获取内容并将其放在容器中

在此先感谢您的帮助。

杰克逊

看一看到window.location对象(documentation)。

如果你得到window.location.pathname参数并执行一些字符串操作(拆分等),你应该很容易得到url的最后'部分'。

查看jQuery .text()将其插入<h3>元素的方法。

这应做到:

var title = location.href.match(/([^\/]*)\/?$/)[1]; //get the last token 
title = title.replace(/[^a-z\d\s]+/ig, ' '); //remove non-alphanumeric characters 
$('h3.urltag').text(title); //set the title to h3 

例子:http://jsbin.com/ubiwo3

+0

+1它肯定会工作.... – Reigel 2010-04-29 04:50:51