toMyCar:119 Uncaught ReferenceError: toJS is not defined at HTMLAnchorElement.onclick

toMyCar:119 Uncaught ReferenceError: toJS is not defined at HTMLAnchorElement.onclick

首先自己遇到这个问题的时候,看一下自己的解决方案
这是a标签的代码

  <a href="javascript:void(0);" class="ifJs" onclick="ifJs();">结算</a>

这是function函数的代码

 //发起结算
    function ifJs() {
        var arr =new Array();
        $(".xzWxz.on").each(function () {
            var id = $(this).parent().attr("data-id");
            console.log(id)
            var num = $(this).parent().find("#count").html();
            console.log(num)
            var obj = new Object();
            obj.id = id;
            obj.num = num;
            arr.push(obj);
        });
        if(arr.length==0){
            alert("至少选择一个商品结算");
            return false;
        }
        $.ajax({
            url : "order",
            type : "POST",
            data :JSON.stringify(arr),
            success : function (result) {
                //还有地址未填写的可能
                if(result.code==100){
                    alert("购买成功")
                }else {
                    alert("请登录");
                    window.location.href = "toLogin";
                }
            }
        });
    }

问题所在:
首先我也是网上搜了下,网上还是有很多常见方法的
自己的原因是把函数写在了$(function(){}); 这个页面加载的函数里,感觉一般都是写在这里面吧
后来显示,这和函数未加载,我就把函数放到了<script type="text/javascript"></script>里面,他就正常了。。。。。。。。。。。

接着我们看看网上的一些可能遇到的问题:

那这是什么原因呢?为什么用 href="javascript:search();" 可以,而用οnclick="search();" 确不行呢?

经过我在努力搜寻,综合国外著名论坛
https://stackoverflow.com/questions/12816400/javascript-typeerror-xxx-is-not-a-function
和国内一些博客,终于找到了答案,总结如下:
主要原因有几点:
1、首先确保这函数的js被引入到了页面;Are you certain you added the script to the page?
2、在调用该方法时,确保该方法已经被加载了。在浏览器控制台输入该方法,能正常运行。 Are you certain it has loaded before you try to call search(); ?
3、使用onclick绑定函数事件时,必须确保href写成 href="javascript:void(0);" 或者 href="javascript:;" 这样,第1种形式中的void表达式中的0不能少。如果少些了0,会报“Uncaught SyntaxError: Unexpected token )”的错误。
4、函数名不能和页面的某个标签的id名相同。一些浏览器可以通过在js代码中指定ID访问节点元素,然后定义的函数就会被 DOM中的元素覆盖了。您需要重命名函数名称或元素ID。(感觉这个也经常有人遇到)

Can you provide the html code, maybe you have an element with id search?
in your html code has ID search just like the name of the function.
some browsers can access the node elements just by specifying the ID in the js code and then the function
defined is overridden by the element in the DOM.
you need to rename the function name or the element ID.

经过审查我的代码,发现我的代码出错就是因为第4点原因,search()函数名和该页面包含的子页面中的一个标签
的id="search"相同了,下面代码为当前的页面包含的一个弹窗页面里的div.

<div id="simple-search" class="simple-search ver_center_fa">                        
    <a id="delContent" class="icon25 icon-delbtns" style="display:none;" οnclick="clearf()"></a>
    <input id="input_ipc" class="search_input" type="text" placeholder="请输入IPC分类号或关键词">
    <a id="search" href="javascript:void(0)" class="btn_search sprite"></a>
</div>

这里有个标签的id=“search”,和我们的方法 search() 同名了,因此引发了冲突了。
解决方法:重命名函数名称或元素id即可。我将该id改为了id=“ipc_search” 就好了。

思考:那为什么我优化之前用 href=“javascript:search();” 可以且没有报错呢,为什么没有与id="search"冲突?

标签的href属性,其中href是hypertext reference的缩略词,用于设定链接地址。链接地址必须为url地址,
如果没有给出具体路径,则默认路径和当前页的路径相同。
而使用href=“javascript:xxx();”,这种写法本身就是不正规的,是不建议的! javascript: 是一个伪协议,其他的伪协议还有 mail: tel: file: 等等。javascript:是表示在触发默认动作时,执行一段JavaScript代码,而 javascript:; 和javascript:void(0);表示什么都不执行,这样点击时就没有任何反应。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值