代码在控制台中工作,但不在头部

时间:2012-05-23 10:33:03

标签: jquery

当我在Google Chrome控制台中输入此脚本时......

<script>
    $(document).ready(function() {  
        $("div").hover(
            function() { $("> span", this).show(); },
            function() { $("> span", this).hide(); 
        });
    });
</script>

......它有效,没问题。

当我在<head></head>之间添加它时,它不起作用。

谁能告诉我为什么?

更新

<script type="text/javascript">!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
     <script type="text/javascript" lang="javascript">
    $(document).ready(function()
{
    $('div').hover(function()
    {
        $('> span', this).show();
    }, function()
    {
        $('> span', this).hide();
    });
});
    </script>
    <script type="text/javascript" lang="javascript">

    $.getJSON("http://twitter.com/statuses/user_timeline/username.json?callback=?&count=5", function(data) {

  $.each(data, function(index, value) { 
 $('.tweets').append('<div id='+data[index].id+'>'+data[index].text+'&nbsp<span style="display:none"class="hideTweeterIcons"><a href="https://twitter.com/intent/tweet?in_reply_to='+data[index].id+'"><img src="//si0.twimg.com/images/dev/cms/intents/icons/reply.png"></a><a href="https://twitter.com/intent/retweet?tweet_id='+data[index].id+'"><img src="//si0.twimg.com/images/dev/cms/intents/icons/retweet.png"></a><a href="https://twitter.com/intent/favorite?tweet_id='+data[index].id+'"><img src="https://si0.twimg.com/images/dev/cms/intents/icons/favorite.png"></a></span></div><hr>');

});     
});

    </script>

2 个答案:

答案 0 :(得分:1)

您提供的代码使用的是jQuery library,请确保在使用jQuery $函数之前包含对库的引用。另外,尝试在脚本标记上指定type,如下所示:

<script type="text/javascript">
    ...
</script>

更新

修改代码后,我注意到hover功能设置不正确,请尝试以下操作:

$(document).ready(function()
{
    $('div').hover(function()
    {
        $('> span', this).show();
    }, function()
    {
        $('> span', this).hide();
    });
});

答案 1 :(得分:0)

你需要确保jQuery代码是你在jQuery库中添加的。如果您使用的是HTML5样板文件,则会出现在页面底部。