Home >Backend Development >PHP Tutorial >javascript - JS直接放在HTML可以执行,单独放到JS文件引用就不行了为啥?

javascript - JS直接放在HTML可以执行,单独放到JS文件引用就不行了为啥?

WBOY
WBOYOriginal
2016-06-06 20:08:121521browse

1.路径没问题
2.js代码使用了jQuery,而且放在了引用文件之后
3.引用js

<code><script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/utils.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/collect.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/sign.js"></script>
</code>

4.直接写在html里

<code><script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/utils.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/collect.js"></script>
<script type="text/javascript">
$(function(){//日期
    var d = new Date();
    var month  = d.getMonth()+1;
    var yestoday = d.getDate()-1;
    var day3 = d.getDate()+2;
    var day4 = d.getDate()+3;
    var day5 = d.getDate()+4;
    var day6 = d.getDate()+5;
    $('.days1').html(month+'.'+yestoday);
    $('.days2').html(month+'.'+day3);
    $('.days3').html(month+'.'+day4);
    $('.days4').html(month+'.'+day5);
    $('.days5').html(month+'.'+day6);
});

$(function(){
    if ({wc:$sign_stetus} == 2) {//正常已签到
        $('.sign').html('已签到{wc:$sign_time}天');
        $('.header_wen1').html('明日可领{wc:$tomorrow_intergral}积分');
        $('.sign_img').attr({"background":"#C8B9A3","color":"#C8B9A3"});
    } else if({wc:$sign_stetus} == 3) {//初始未签到
        $('.header_wen1').html('今日可领5积分');
        $('.1').html(+0);
        $('.2').html(+5);
        $('.3').html(+5);
        $('.4').html(+10);
        $('.5').html(+10);
        $('.6').html(+15);
        $('.7').html(+15);
    } else if({wc:$sign_stetus} == 4) {//初始已签到
        $('.sign').html('已签到1天');
        $('.header_wen1').html('明日可领5积分');
        $('.sign_img').attr({"background":"#C8B9A3","color":"#C8B9A3"});
        $('.1').html(+0);
        $('.2').html(+5);
        $('.3').html(+5);
        $('.4').html(+10);
        $('.5').html(+10);
        $('.6').html(+15);
        $('.7').html(+15);
    } else if({wc:$sign_stetus} == 5) {
        $('.sign').html('已签到{wc:$sign_time}天');
        $('.header_wen1').html('明日可领{wc:$tomorrow_intergral}积分');
    }
});
</script></code>

回复内容:

1.路径没问题
2.js代码使用了jQuery,而且放在了引用文件之后
3.引用js

<code><script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/utils.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/collect.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/sign.js"></script>
</code>

4.直接写在html里

<code><script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/utils.js"></script>
<script type="text/javascript" src="%7BG_TEMPLATES_STYLE%7D/js/collect.js"></script>
<script type="text/javascript">
$(function(){//日期
    var d = new Date();
    var month  = d.getMonth()+1;
    var yestoday = d.getDate()-1;
    var day3 = d.getDate()+2;
    var day4 = d.getDate()+3;
    var day5 = d.getDate()+4;
    var day6 = d.getDate()+5;
    $('.days1').html(month+'.'+yestoday);
    $('.days2').html(month+'.'+day3);
    $('.days3').html(month+'.'+day4);
    $('.days4').html(month+'.'+day5);
    $('.days5').html(month+'.'+day6);
});

$(function(){
    if ({wc:$sign_stetus} == 2) {//正常已签到
        $('.sign').html('已签到{wc:$sign_time}天');
        $('.header_wen1').html('明日可领{wc:$tomorrow_intergral}积分');
        $('.sign_img').attr({"background":"#C8B9A3","color":"#C8B9A3"});
    } else if({wc:$sign_stetus} == 3) {//初始未签到
        $('.header_wen1').html('今日可领5积分');
        $('.1').html(+0);
        $('.2').html(+5);
        $('.3').html(+5);
        $('.4').html(+10);
        $('.5').html(+10);
        $('.6').html(+15);
        $('.7').html(+15);
    } else if({wc:$sign_stetus} == 4) {//初始已签到
        $('.sign').html('已签到1天');
        $('.header_wen1').html('明日可领5积分');
        $('.sign_img').attr({"background":"#C8B9A3","color":"#C8B9A3"});
        $('.1').html(+0);
        $('.2').html(+5);
        $('.3').html(+5);
        $('.4').html(+10);
        $('.5').html(+10);
        $('.6').html(+15);
        $('.7').html(+15);
    } else if({wc:$sign_stetus} == 5) {
        $('.sign').html('已签到{wc:$sign_time}天');
        $('.header_wen1').html('明日可领{wc:$tomorrow_intergral}积分');
    }
});
</script></code>

你这里的代码有dom操作,对于dom的操作必须在相应的dom加载完才能操作要不然$(selecter)会出会是个空的对象;
含有dom操作的代码可以放到文档末尾,或者使用jquery.ready函数

里边是不是有服务端的标签?js文件中服务端标签没法替换

F12看有没有报错

这就像jsp页面上的js,直接使用el表达式没问题,外部引用写el无效一个道理,1楼正解

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn