为什么这个JavaScript代码与其他JavaScript插件一起冲突?

时间:2012-03-08 06:28:42

标签: php jquery html

我的代码是网站计数器(counter.php)。它在直接运行时非常有效,这意味着 每次刷新页面时,计数器从5754开始递增;但是,当counter.php包含在包含其他jQuery插件的另一个页面中时,它将停止工作。计数器是静态的;它会一直显示5754

请帮助我并澄清我哪里出错。

此部分是增量(访客数量)发生的地方:

<?php
session_start();  
if(isset($_SESSION['executed']))
$_SESSION['executed'] = $_SESSION['executed']+ 1;
else
$_SESSION['executed'] = 5754;
?>

页面本身,这是自我解释的:      

<head> 
<script src="counter/js/jquery-1.7.1.j"> type="text/javascript"></script>  

柜台离开的地方:

<div class="counter-wrap"> 

    <div class="counter-number">
        &nbsp;
    </div>
</div>

<div>

内联CSS:

<style>

.counter-wrap {
    height:18px;
    overflow:hidden;   
}

.counter-number {
    height:198px;
    width:12px;
    position:relative;
    background-image:url(http://developer.mindtouch.com/@api/deki/files/4548/=counter_ticker_bg.gif);  
    float:left;
}

</style>

现在是jQuery代码:

<script type="text/javascript">
$(".counter-number").each( function(i) {
    $(this).attr('id','num'+i);    
});

function loadinput() {
    var newval = $("#numgo").val();
    loadticker(newval);
}

function loadticker(ticnum) {
    var fticnum = add_commas(ticnum);
    var numheight=18;
   addticker(fticnum);
    if (ticnum && ticnum != 0) {

        var s = String(fticnum);

        for (i=s.length;i>=0; i--)
        {
            var onum=s.charAt(i);          
            $("#num"+i).attr('value',onum);
        }

        $(".counter-number").each( function() {
            var nval=$(this).attr("value");
            if (!isNaN(nval)) {
                var nheight = Number(nval)*numheight*-1;
                $(this).animate({ top: nheight+'px'}, 1500 );
            }
            if (nval==','){
                $(this).animate({ top: '-180px'}, 1500 );
            }
        });
    }
}

function addticker(newnum) {
    var digitcnt = $(".counter-number").size();
    var nnum = String(newnum).length;
    var digitdiff = Number(nnum - Number(digitcnt));
    if (digitdiff <0) {
        var ltdig = (Number(nnum)-1);
        $(".counter-number:gt(" + ltdig + ")").remove();
    }

    for(i=1;i<=digitdiff;i++) {
        $(".counter-wrap").append('<div class="counter-number" id="num' + (Number(digitcnt+i-1)) + '">&nbsp;</div>');
    }

}

function add_commas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');

    }
    return x1 + x2;

}


</script>

</head>

这是调用函数的地方:

<body onload="loadticker( '<?php echo $_SESSION['executed'];  ?>' ) ">

</body> 

</html>

1 个答案:

答案 0 :(得分:1)

在我看来,这不是一个jQuery问题。我建议您尝试在代码顶部添加PHP代码,甚至在<html>打开代码之前。

试试这个:

<?php
session_start();  
if(isset($_SESSION['executed'])) {
    $_SESSION['executed'] = $_SESSION['executed']+ 1;
} else {
    $_SESSION['executed'] = 5754;
}

echo $_SESSION['executed'];

?>
<html>
<head></head>
...
<body onload="loadticker('<?php echo $_SESSION['executed']; ?>')">
</body>
</html>