javascript - $(位置).attr ('href' );不工作

标签 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;
}

最佳答案

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

假设您正在谈论 window.location而不是其他东西。

Returns a Location object, which contains information about the URL of the document and provides methods for changing that URL. You can also assign to this property to load another URL.

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

所以你的代码会像这样:

var currentUrl = window.location.href;

关于javascript - $(位置).attr ('href' );不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5474121/

相关文章:

javascript - React-Native:更改 ImageBackground 的不透明颜色

javascript - Android Honeycomb 模拟器上的 Google 图表轴渲染问题

javascript - 将动态 css 样式应用于 Oracle jet 中的下拉列表项

javascript - jQuery 检查何时 PDF 下载完成

javascript - 使用 webpack 在输出目录中保留文件夹结构

javascript - Bootstrap 3 DateTimePicker v4 禁用月份

javascript - 库存工具的导航箭头

javascript - 如何用python创建highcharts格式的json结构

javascript - Github API v3 : Update file not working (404)

javascript - 尝试将 $interval 与全局变量转换为函数