简体   繁体   English

程式无法在Google Chrome上运作

[英]Program not working on Google Chrome

I am programming a sudoku puzzle ( http://www.jsfiddle.net/sZ7Aq/4/ ). 我正在编写数独谜题( http://www.jsfiddle.net/sZ7Aq/4/ )。 It works okay on IE, but when I try it on Google Chrome, the button doesn't do anything when I click on it. 它在IE上可以正常运行,但是当我在Google Chrome上尝试使用该按钮时,单击该按钮不会执行任何操作。 Is there a way to fix it so it works on all browsers? 有没有一种方法可以解决它,使其在所有浏览器上都能正常工作?

Please note: I haven't finished it so there isn't a puzzle generating function. 请注意:我尚未完成,所以没有拼图生成功能。 You must enter all numbers yourself. 您必须自己输入所有数字。

Here is my main() function (if you did not click on the link yet): 这是我的main()函数(如果您尚未单击链接):

function main() {
    getcellVal();
    if (validate() == false) {
        alert("Something's not right!");
        return false;
    }
    alert("Good job!");
    return true;
}

My button: 我的按钮:

<button onclick="javascript: main()">Check my answer</button>

Use .addEventListener('click', main); 使用.addEventListener('click',main); For instance: 例如:

var check = document.getElementById('check');
check.addEventListener('click', main);

and of course add id="main" and remove the onclick from your button. 当然添加id =“ main”并从按钮中删除onclick。

Updated fiddle 更新的小提琴

Then open your console and fix the other bugs... 然后打开控制台并修复其他错误...

From your jsfiddle, the error is in your getcellValue() function. 在您的jsfiddle中,错误出在您的getcellValue()函数中。 This code is incorrect: 此代码不正确:

colVal  =  [[x0y0.value, x0y1.value, x0y2.value, x0y3.value, x0y4.value, x0y5.value, x0y6.value, x0y7.value, x0y8.value],

I would suggest you change your input elements to have an id attribute that matches the name. 我建议您将输入元素更改为具有与名称匹配的id属性。 Like this: 像这样:

<input type="text" name="x0y0" id="x0y0">

Then change your getcellVal() function to use the getElementById function. 然后将您的getcellVal()函数更改为使用getElementById函数。

colVal  =  [[document.getElementById("x0y0").value, ...],

It will be much more verbose, but it will work in more browsers. 它将更加冗长,但可以在更多浏览器中使用。

Few things need to be corrected: 几件事需要纠正:

  • Instead of defining an event such as javascript: main() inline, use unobtrusive solution to register the necessary event handlers programmatically. 与其定义一个事件(例如javascript: main()内联),不如使用非侵入式解决方案以编程方式注册必要的事件处理程序。

Eg: 例如:

<button id="buttonid">Check my answer</button>

window.onload = function() {
document.getElementById('buttonid').onclick = main;
};
  • You cannot access the DOM elements just by specifying their names. 您不能仅通过指定DOM元素的名称来访问它们。 In your case x0y0.value will not return anything. 在您的情况下, x0y0.value将不返回任何内容。 Instead use id or class name to access the set of elements. 而是使用idclass名来访问元素集。

Eg: 例如:

<input type="text" name="x0y0" id="x0y0">

In javascript, 在javascript中,

document.getElementById("x0y0").value

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

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