$(位置).attr( 'href' 属性);不工作

时间:2011-03-29 14:26:10

标签: javascript

我不知道为什么但这个代码有问题。横幅显示在每个页面上,虽然它已经指定了属性$(location).attr('href')你可以帮助我吗?:

<div id="bottombar">
<div class="bottom-content">
<a href="http://www.cliente.org/" target="_blank"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/banner/bottom-logo.png" alt="player-logo" /></a>
<a href="http://www.cliente.org/" target="_blank"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/banner/bottom-txt.png" alt="player-slogan" /></a>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/banner/bottom-download.png" alt="player-download" />
</div>
<div id="bottombarClose"><p>Chiudi</p></div>
<script type="text/javascript"">
$(document).ready(function() {
var currentUrl = $(location).attr('href');
if(currentUrl == 'http://www.esempio.net/') 
$('#bottombar').show();
$("#bottombarClose").click(function() {
$('#bottombar').hide();
});
});
</script>
</div>

CSS代码:

  div#bottombar {
  background-image: url(images/banner/player-bg3.png);
  background-repeat: repeat-x;
  bottom: 0;
  color: #FFFFFF;
  font-family: Arial,Helvetica,sans-serif;
  height: 100px;
  left: 0;
  margin: 0;
  position: fixed !important;
  width: 100%;
  z-index: 99999;
  display:none;
}

.bottom-content {
  bottom: 0;
  height: 97px;
  left: 50%;
  margin-left: -495px;
  position: absolute;
  width: 960px;
  z-index: 10;
}

#bottombarClose {
  cursor: pointer;
  float: right;
  padding: 55px 10px 0 0;
}

6 个答案:

答案 0 :(得分:5)

你的意思不只是location.href吗?

假设您正在讨论window.location而不是其他内容。

  

返回一个Location对象   包含有关URL的信息   该文件并提供方法   更改该网址。你也可以分配   到此属性加载另一个URL。

位置不是dom 中的元素,因此jQuery无法选择它。

所以你的代码就是这样:

var currentUrl = window.location.href;

答案 1 :(得分:2)

location不是DOM Element,而是Location object。 您正在尝试从中创建一个jQuery对象,但它没有意义。

请改用:

var currentUrl = window.location.href;

答案 2 :(得分:1)

试试这个

 var currentUrl = window.location.href;

答案 3 :(得分:0)

请尝试使用var currentUrl = window.location.href。这将返回当前页面的位置。

答案 4 :(得分:0)

而不是

$(location).attr('href'); 

使用

currentUrl = location;

答案 5 :(得分:0)

好吧,我以这种方式做了改变,但似乎没有用:

$(document).ready(function() {
var currentUrl = window.location.href;
if(currentUrl == 'http://streamingdb.net/')  
$('#bottombar').show(); 
$("#bottombarClose").click(function() {
$('#bottombar').hide();
});
});

然后我给div#Bottombar添加了一个“display:none”,一切正常。感谢您的所有时间。

相关问题