jQuery可以检测特定的URL并根据该URL执行功能吗?

时间:2011-01-06 20:39:46

标签: jquery

我有一个我正在研究的网站。如果URL与特定URL匹配,我需要能够执行特定功能。以下是我正在尝试做的一个例子:

如果网址与此网址匹配:

http://www.example.com/EIFS-items/search.php?l2=3,15,25

然后我想让jQuery在div中添加一个“show”类,下面是“content”类。

这可能吗?

<html>
<head>
<title>Title</title>
<style type="text/css">
.content {
display: none;
}
.show {
display: block;
}
</style>
</head>
<body>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</div><!-- end .content -->
</body>
</html>

3 个答案:

答案 0 :(得分:7)

var url = "http://www.example.com/EIFS-items/search.php?l2=3,15,25";
$(function(){
  if (location.href==url){
    $('.content').show();
  }
});

答案 1 :(得分:2)

您似乎在寻找location object,其中包含有关当前网页网址的信息。

答案 2 :(得分:1)

使用查询字符串location.search可能比location.href更好。这将涵盖子域/协议更改。

if(location.search == "?l2=3,15,25")
    $('.content').addClass('show');