0

HTML:

<div id="text" class="text">
   Hello World! <a href="#hide">CLOSE</a>
</div>

脚本:

$(function() {
    if ( document.location.href.indexOf('#hide') > -1 ) {
      $('#text').fadeOut('slow');
    }
});

单击后不会隐藏“文本”div。如果我直接去“www.mysite.com/#hide”,它会起作用。

4

2 回答 2

2

You want to select the link with the href="#hide"? Use the attribute-equals selector:

$(function() {
    $("a[href='#hide']").click(function() {  $('#text').fadeOut('slow');});
});

The link itself would be treated as an local anchor (means on the same page). This won't cause the browser to reload the page and therefore your script won't be executed. In modern browsers you could capture these changes via the hasChange event.

于 2013-02-02T20:34:08.593 回答
0

您是在单击功能上调用它吗?例如

var hashHide = function() {
    if ( document.location.href.indexOf('#hide') > -1 ) {
      $('#text').fadeOut('slow');
    }
};

$('itemClicked').live('click', function(){hashHide()}));

我意识到 .live() 功能已被贬值......但是坏习惯:)

于 2013-02-02T19:54:55.270 回答