javascript $(location).attr('href'); 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5474121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:19:08  来源:igfitidea点击:

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

javascript

提问by Fask

I do not know why but I have problems with this code. The banner is displayed on every page although it has specified the attribute $(location).attr('href') you can help me?:

我不知道为什么,但我对这段代码有问题。横幅显示在每个页面上,尽管它指定了属性 $(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 Code:

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

回答by Mark Coleman

Don't you mean just location.href?

你不是说只是location.href吗?

This is assuming you are talking about window.locationand not something else.

这是假设你在谈论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.

返回一个 Location 对象,其中包含有关文档 URL 的信息并提供更改该 URL 的方法。您还可以分配给此属性以加载另一个 URL。

Location is not an element in the domthus jQuery can not select it.

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

So your code would be like so:

所以你的代码应该是这样的:

var currentUrl = window.location.href;

回答by bpierre

locationis not a DOM Element, it is a Location object. You are trying to create a jQuery object from it, but it does not make sense.

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

Use this instead:

改用这个:

var currentUrl = window.location.href;

回答by Headshota

Try this

试试这个

 var currentUrl = window.location.href;

回答by tvkanters

Try using var currentUrl = window.location.hrefinstead. This will return the location of the current page.

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

回答by jimy

Instead of

代替

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

use

利用

currentUrl = location;

回答by Fask

ok, I made ??the change in this way, but did not seem to work:

好的,我以这种方式进行了更改,但似乎不起作用:

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

then I added a "display:none" to div#Bottombar and everything works. Thanks to all of your time.

然后我向 div#Bottombar 添加了一个“显示:无”,一切正常。感谢您的所有时间。