1

I'm using jQuery for selecting an element that is targeted (using #test in the url) and I'm getting a browser-dependent behavior with it.

The code:

### 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>

I tried using jsFiddle but this requires the url (which I tried but I was not able to make it work in jsFiddle).

Now, if I open the page test.html#test...

  • with firefox 24.0, it alerts an Hello world and fades correctly.
  • with Chrome 29.0.1547.76, it alerts an undefined, and doesn't fade.

I'm using Mac OS X in both.

What am I doing wrong? is this a "feature"?

4

1 回答 1

1

似乎是一个错误,您可以使用哈希作为选择器来解决它:

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

或者做一段时间直到你的元素存在或者像评论中提到的凯文一样到达一段时间。

编辑:错误报告(有一些重复)http://bugs.jquery.com/ticket/14135

于 2013-09-23T20:02:44.580 回答