2013-09-23 32 views
1

我使用jQuery選擇一個目標元素(在URL中使用#test),我得到一個依賴於瀏覽器的行爲。問題與「:目標」選擇器在jQuery 1.10.2和鉻

代碼:

### test.html 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script src="[path_to_]/jquery-1.10.2.min.js"></script> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 
<body> 
<div id="test">Hello world</div> 
<script> 
    $(document).ready(function() { 
     alert($(":target").html()); 
     $(":target").fadeOut(1000).fadeIn(1000); 
    }); 
</script> 
</body> 
</html> 

我嘗試使用jsFiddle但是這需要的URL(我試過,但我沒能使其在正常工作的jsfiddle)。現在

,如果我打開網頁test.html#test ...

  • 與Firefox 24.0,它提醒了Hello world和 正確變淡。
  • 與Chrome 29.0.1547.76,它提示undefined, 不褪色。

我在兩者中都使用Mac OS X.

我在做什麼錯?這是一個「功能」?

+0

就像一個測試,如果你換你的警覺和動畫在setTimeout的,比方說,在1秒鐘之後,它的工作原理? –

+0

只有1毫秒的超時解決方法解決了該錯誤。 – eloyesp

回答

1

似乎是一個錯誤,你可以用任何使用哈希作爲選擇變通辦法:

$(document).ready(function() { 
    alert($(location.hash).html()); 
    $(location.hash).fadeOut(1000).fadeIn(1000); 
    }); 

,或者直到你的元素存在或達到一定的時間像凱文註釋中做了一段時間。

編輯:錯誤報告(也有少數重複)http://bugs.jquery.com/ticket/14135