简体   繁体   English

简单功能无法在Chrome和Chromium中使用

[英]Simple function not working in Chrome and Chromium

This simple piece of chode is not working in Chromium(Ubunto) and Chrome. 这个简单的杂项在Chromium(Ubunto)和Chrome中不起作用。

HTML 的HTML

<input type="button" id="saveFavPunch" name="saveFavPunch" value="save" onClick="saveFavPunch()" >

SCRIPT 脚本

var req;
function saveFavPunch(){
    alert('govind singh');
    if(!req){
        req=$.ajax({
            type:"POST",                
            url:"edit?editType=saveFavPunch",
            data: {"value":document.getElementById("punchId").value},
            complete:function(){req=false},
            success: function(data){
                $.fancybox.close();
                if(data==""){
                    alert("ERROR!");
                }else{
                if(data=="0"){
                    alert("Internal Error Occurs, please try after some time");
                }else{
                    document.getElementById("favPunchline").innerHTML=data;
                }
                }                       
            }//end success
        });
    }
}

Can't add a comment yet, nothing's wrong with the code, but to make it work in jsfiddle change onLoad to noWrap 现在还不能添加注释,代码没什么问题,但是要使其在jsfiddle中工作,请将onLoad更改为noWrap

Description of what each of the settings does: http://doc.jsfiddle.net/basic/introduction.html#frameworks-and-extensions 每个设置的作用说明: http : //doc.jsfiddle.net/basic/introduction.html#frameworks-and-extensions

Basically you need it to be in a simple <script> tag, not in the onLoad event 基本上,您需要将其放在简单的<script>标记中,而不是在onLoad事件中

Fiddle with the code you provided: http://jsfiddle.net/Sam88/YEPm3/12/ 摆弄您提供的代码: http : //jsfiddle.net/Sam88/YEPm3/12/

Since you mentioned jQuery in your tags, I thought I'd give a jQuery solution: Fiddle 既然您在标签中提到了jQuery,我想我会提供一个jQuery解决方案: Fiddle

Html: HTML:

<input type="button" id="saveFavPunch" name="saveFavPunch" value="save" />

Javascript: Javascript:

$("#saveFavPunch").on("click", function () {
    alert('hello2');
});

:) :)

Edit 编辑

Javascript Not Running On JSFiddle is a possible duplicate of your question. Javascript未在JSFiddle运行可能与您的问题重复。 I am quoting the selected answer: 我引用了选定的答案:

The functions you define are defined in an onload function, so whereas before they were referenceable, because they are defined in that function they can only be referenced from within that function. 您定义的函数是在onload函数中定义的,因此在它们可被引用之前,因为它们是在该函数中定义的,所以只能在该函数中引用它们。 You reference them as globals in your HTML. 您在HTML中将它们称为全局变量。 You have three options 您有三个选择

a) ( easiest, quickest, not ideal ) - change function blah(){} to window.blah = function(){}; a)(最简单,最快,最不理想)-将函数blah(){}更改为window.blah = function(){}; making the functions global. 使功能全局化。

b) ( ideal way ) - use unobtrusive Javascript to attach behaviour to DOM elements from within the JS solely, meaning separate HTML from JS. b)(理想的方式)-使用不引人注目的Javascript将行为仅从JS内附加到DOM元素,这意味着将HTML与JS分开。

c) Make the jsfiddle not wrap the stuff onload. c)使jsfiddle不包装东西。 Change onLoad to no wrap ( body or head ). 将onLoad更改为不环绕(身体或头部)。

So instead of you'd do var e = document.getElementById('foo'); 因此,代替您执行var e = document.getElementById('foo'); e.onclick = lol; e.onclick =哈哈; in the JS only. 仅在JS中。

I recommend b as it encourages best practices. 我推荐b,因为它鼓励最佳做法。

Just one change — JSFiddle setting from "onLoad" to "No wrap - in " and it works: 只需更改一下即可-将JSFiddle设置从“ onLoad”更改为“ No wrap-in”,它可以工作:

http://jsfiddle.net/3f7PT/ http://jsfiddle.net/3f7PT/

The problem with the onLoad option is that this is what it outputs in the results: onLoad选项的问题在于,这是它在结果中输出的内容:

//<![CDATA[ 
    window.onload=function(){
        function saveFavPunch(){
            alert('hello2');
        }
}//]]>

So your function gets unintentionally wrapped in another function which stops it being found from that onclick call. 因此,您的函数被无意间包装在另一个函数中,这会阻止从该onclick调用中找到它。

Hope this helps. 希望这可以帮助。

The following entire script works properly in Chrome for me. 以下整个脚本对我来说在Chrome中正常运行。

<html>
<head>
<script>
function saveFavPunch(){
    alert('hello2');
}
</script>
</head>
<body>
<input type="button" id="saveFavPunch" name="saveFavPunch" value="save" onClick="saveFavPunch()" >
</body>

This works 这有效

document.getElementById("saveFavPunch").addEventListener("click", saveFavPunch);
function saveFavPunch(){
    alert('hello2');

}

http://jsfiddle.net/6sg9R/ http://jsfiddle.net/6sg9R/

Ye JSFiddle is very finicky about Alerts. Ye JSFiddle对Alerts非常挑剔。 It works no problem as Spassvogel shows but your way should work as well from file as opposed to on JSfiddle. 正如Spassvogel所显示的那样,它没有任何问题,但是从文件开始,而不是在JSfiddle上,您的方式应该可以正常工作。

Even those you having this type of problem then use div tag 即使是您遇到此类问题的人,也请使用div标签

    <div id="saveFavPunch" style="height:30px; widht:100px; display:block;" onClick="saveFavPunch()">Save</div> 

I think Using this code Your problem will solve. 我认为使用此代码可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM