前端HTML代码编写中出现错误和改错方法

近期开始进行前端代码编写本文将记录部分编写过程中出现问题和解决过程(本文动态更新):

1、Uncaught TypeError: $(...).live is not a function

原因:jquery后续版本取消.live的相关代码,采用新版jquery(1.7)以后的找不到该函数,意思就是  找不到live这个函数了。

说明:is not a function是指错误类型,live是指错误的函数名称

采用直接将live改成.on就消除错误:代码如下:

原出错代码:

$(".body_sanji_div_bor >li").live({
    mouseover: function () {
        var id = $(this).attr('id');
        var pid = $(this).attr('pid');
        /*↓↓触摸状态判断↓↓*/
        $(".body_sanji_div[pid='"+pid+"']").next().remove();
        var cahndu1 = titleLength(jinru_id);
        var cahndu2 = titleLength(id);
        if(cahndu1 > cahndu2){
            // 回退
            // console.log("回退");
            $(".body_sanji_div[pid='"+pid+"']").next().remove();
        }
        /* ↑↑触摸状态判断↑↑*/

        jinru_id = id;
        var ju_top = $(this).offset().top;
        ju_top = ju_top-60;
        var shifou_xiaji = 0;
        for (var i=0;i<menuJson.length;i++){
           if(menuJson[i]['pid'] == id){
               shifou_xiaji = 1;
           }
        }
        if(shifou_xiaji == 1){
            var pid = 0 ;
            for (var i=0;i<menuJson.length;i++){
                if(menuJson[i]['id'] == id){
                    pid = menuJson[i]['pid'];
                }
            }
            var bor_html = html_wuxian_bor(id,ju_top,'on');
            $(".body_sanji_div[pid='"+pid+"']").parent('.body_sanji').append(bor_html);
            for(var x in menuJson){
                if(menuJson[x]['pid'] == id){
                    var wuxian_data = html_wuixan_li(menuJson[x]['id'],menuJson[x]['pid'],menuJson[x]['name'],menuJson[x]['url']);
                    $(".body_sanji_div[pid='"+id+"']").children(".body_sanji_div_bor").append(wuxian_data);
                }
            }
        }
    },
    mouseout: function () {

    }
});

错误如下图:

修改后的代码只在第一行修改live->on:

$(".body_sanji_div_bor >li").on({
    mouseover: function () {
        var id = $(this).attr('id');
        var pid = $(this).attr('pid');
        /*↓↓触摸状态判断↓↓*/
        $(".body_sanji_div[pid='"+pid+"']").next().remove();
        var cahndu1 = titleLength(jinru_id);
        var cahndu2 = titleLength(id);
        if(cahndu1 > cahndu2){
            // 回退
            // console.log("回退");
            $(".body_sanji_div[pid='"+pid+"']").next().remove();
        }
        /* ↑↑触摸状态判断↑↑*/

        jinru_id = id;
        var ju_top = $(this).offset().top;
        ju_top = ju_top-60;
        var shifou_xiaji = 0;
        for (var i=0;i<menuJson.length;i++){
           if(menuJson[i]['pid'] == id){
               shifou_xiaji = 1;
           }
        }
        if(shifou_xiaji == 1){
            var pid = 0 ;
            for (var i=0;i<menuJson.length;i++){
                if(menuJson[i]['id'] == id){
                    pid = menuJson[i]['pid'];
                }
            }
            var bor_html = html_wuxian_bor(id,ju_top,'on');
            $(".body_sanji_div[pid='"+pid+"']").parent('.body_sanji').append(bor_html);
            for(var x in menuJson){
                if(menuJson[x]['pid'] == id){
                    var wuxian_data = html_wuixan_li(menuJson[x]['id'],menuJson[x]['pid'],menuJson[x]['name'],menuJson[x]['url']);
                    $(".body_sanji_div[pid='"+id+"']").children(".body_sanji_div_bor").append(wuxian_data);
                }
            }
        }
    },
    mouseout: function () {

    }
});

结果:错误消除如图:

2、Uncaught ReferenceError: verorccc is not defined at style.js:formatted:311:25 

原因:代码行中输入了一个未知的参数

说明:Uncaught ReferenceError是指错误类型,verorccc是指错误的名称,is not defined是指错误的说明,style.js:formatted是指错误的脚本文件名,311:25是指我们错误的行,列坐标 

本人采用直接将verorccc改成sanji_i就消除错误:

错误代码如下:

console.warn('warn输出' + verorccc);

出现如图错误:

 改错后代码:

var testvel = 123456;
console.warn('warn输出' + testvel);

经过测试输出来的信息就没有错误了如下图所示: 

 3、Uncaught SyntaxError: Unexpected identifier  style.js:formatted:392 

原因:代码行中一行代码中调用两次的函数没有用符号去隔开

说明:Uncaught SyntaxError是指错误类型,Unexpected identifier是指错误的说明意思是错误定义一般都是没有正确的使用必须的符号隔开2行代码 ,style.js:formatted是指错误的脚本文件名,392 是指我们错误的行,这边列出下列几种常见的

3.1、错误代码没有用隔开如下:

console.clear();
var testvel = 123456;
console.log('warn输出' + testvel)console.log('warn输出' + testvel);

错误如下图:

 本人采用两种方法消除错误:

一、如下代码加上分号;同时回车一次这样语法比较好的习惯,也是不会出现莫名错误的

console.clear();
var testvel = 123456;
console.log('warn输出' + testvel);
console.log('warn输出' + testvel);

二、如下代码全部回车处理,没有加分号,这个强烈不推荐的方法,万一用压缩软件压缩后可能就悲剧了浏览器也识别回车键

console.clear();
var testvel = 123456
console.log('warn输出' + testvel)
console.log('warn输出' + testvel)

运行的结果均没有出现错误如下图:

3.2、错误情形代码没有符号去隔开如下:

var testvar = {
    name: 'testname'
    family: {
        name:'testfamily'
    }
}

var te = testvar.family.name;
var testend='测试到这边为止'

错误如图所示:

 改正错误代码如下:

var testvar = {
    name: 'testname',
    family: {
        name:'testfamily'
    }
}

var te = testvar.family.name;
var testend='测试到这边为止'

4、Uncaught SyntaxError: Unexpected end of input     style.js:formatted:568 

原因:代码行中函数调用没有使用对应的大括号同样的文件名和行号

注意:这边行号往往不是准确的知识和你说到了文件结尾都没有找到函数结尾,处理这样错误就需要往前找,系统提示的错误信息只是个大概了。代码如下:

function fn() {

console.clear();
var testvel = 123456;
console.log('warn输出' + testvel);
console.log('warn输出' + testvel);

错误如图:

 在388行的代码上开始的函数符号错误的行号提示是568行,得往前找180行的代码。迷惑性较大

修改代码:

function fn() {
    console.clear();
    var testvel = 123456;
    console.log('warn输出' + testvel);
    console.log('warn输出' + testvel);
}
fn();

运行结果如下图就修正完成:

5、Uncaught SyntaxError: Unexpected token '}'   style.js:formatted:392 

原因:代码行中函数结尾多了一个符号与前面函数{不配对,

注意:这边错误提示行号是结尾多余的地方,有时候删除就可以,有时候就得找出你需要开始的对应的方添加{符号错误代码如下:

function fn(testvel) {
    console.clear();
    console.log('warn输出' + testvel);
    console.log('warn输出' + testvel);
}}

fn();

运行结果错误如下图所示:

改错过程如下2种:

一、删除多余的符号如下代码:

function fn(testvel) {
    console.clear();
    console.log('warn输出' + testvel);
    console.log('warn输出' + testvel);
}

fn();

运行结果如下图:

 二、补上前面缺失的符号代码如下:

function fn(testvel) {{
    console.clear();
    console.log('warn输出' + testvel);
    console.log('warn输出' + testvel);
}}


fn();

运行结果如图所示: 

6、Uncaught TypeError: Cannot read properties of undefined (reading 'name') style.js:formatted:321

原因:代码行中使用一个变量中没有的成员,

注意:要想取出变量成员的值需要正确的变量名和成员名

错误代码如下:

var testvar = {
    name: 'testname',
    family: {
        name:'testfamily'
    }
}

var te = testvar.ff.name;

结果错误界面如下图:

 修复代码如下:

注意:修复代码中有2个name,部分的浏览器不支持会报错误。产生Uncaught SyntaxError: Identifier 'name' has already been declared的错误,本文中使用的谷歌浏览器能自动辨别相关类型在测试过程中没有报错。 

var testvar = {
    name: 'testname',
    family: {
        name:'testfamily'
    }
}

var te = testvar.family.name;

运行结果如下图:

 7、 Uncaught SyntaxError: Unexpected token ';'    style.js:formatted:389

 原因:在代码中定义时候错误使用符号了也就是语法不对

看看错误代码如下:

var testvar = {
    name: 'testname';
    family: {
        name:'testfamily'
    }
}

var te = testvar.family.name;
var testend='测试到这边为止'

运行如下图错误:

 除错的过程:将错误的符号改成错误排除看如下代码:

var testvar = {
    name: 'testname',
    family: {
        name:'testfamily'
    }
}

var te = testvar.family.name;
var testend='测试到这边为止'

运行结果如下图:

 8.Uncaught SyntaxError: Invalid or unexpected token  style.js:formatted:389

 原因:代码中错误的把‘,’符号打成中文字符的‘

注意:这个问题不容易排查出来,在有使用中文场景时候常误换进去的排查时候很难看出二者的区别。提示里面也没有这个符号相当诡异的错误

错误代码如下:

var testvar = {
    name: 'testname',
    family: {
        name:'testfamily'
    }
}

var te = testvar.family.name;
var testend='测试到这边为止'

运行错误如下图所示:

 修改后正确代码:

var testvar = {
    name: 'testname',
    family: {
        name:'testfamily'
    }
}

var te = testvar.family.name;
var testend='测试到这边为止'

运行结果如下图:

Uncaught RangeError: Maximum call stack size exceeded  style.js:formatted:315

原因:代码中函数调用层次过多,导致栈空间不足了,超过了栈空间的限制

注意:这个错误消息出来后展开错误消息是有相关函数调用的相关顺序信息的,排错是后可以逐步 对应进行排查错误。

下面代码简单的演示了栈溢出的代码实际出现这个错误时候远比演示代码要复杂: 

function testfen() {
    var testvar = 'test';
    testfen();
}
testfen();
var testend='测试到这边为止'

运行错误如下图: 

  修该后正确的代码如下:

function testfen() {
    var testvar = 'test';
    //testfen();去掉自己递归调用的功能就可以了
}
testfen();
var testend='测试到这边为止'

得到结果如下图:

 码字不易,当你有所获不妨点个赞,或收藏一下!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然,我会帮你改正代码错误。以下是修改后的代码: ```c #include <stdio.h> // 定义基类 Shape typedef struct { int x; int y; } Shape; // 定义派生类 Rectangle typedef struct { Shape shape; // 继承基类 Shape int length; int width; } Rectangle; // 定义派生类 Circle typedef struct { Shape shape; // 继承基类 Shape int radius; } Circle; // 定义派生类 Square typedef struct { Rectangle rectangle; // 继承派生类 Rectangle } Square; // 计算矩形面积 int getRectangleArea(Rectangle* rect) { return (rect->length) * (rect->width); } // 计算圆面积 float getCircleArea(Circle circle) { return 3.14 * circle.radius * circle.radius; } // 计算正方形面积 int getSquareArea(Square square) { return square.rectangle.length * square.rectangle.length; } int main() { Rectangle rect; Circle cir; Square sqr; // 输入矩形的长和宽 printf("请输入矩形的长和宽:"); scanf("%d%d", &rect.length, &rect.width); printf("矩形的面积为:%d\n", getRectangleArea(&rect)); // 输入圆的半径 printf("请输入圆的半径:"); scanf("%d", &cir.radius); printf("圆的面积为:%f\n", getCircleArea(cir)); // 输入正方形的边长 printf("请输入正方形的边长:"); scanf("%d", &sqr.rectangle.length); printf("正方形的面积为:%d\n", getSquareArea(sqr)); return 0; } ``` 在修改后的代码,我修复了以下问题: 1. 修复了`getRectangleArea`函数的声明,将参数类型改为`Rectangle*`。 2. 修复了`getRectangleArea`函数的实现,使其正确计算矩形的面积。 3. 在`main`函数,将`getRectangleArea(&rect)`作为参数传递给`printf`函数,以正确输出矩形的面积。 请尝试运行修改后的代码,看看是否满足你的需求。如果还有其他问题,请随时告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坊垚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值