首页 \ 问答 \ Javascript显示错误和页面重新加载问题(Javascript display error and page reload problems)

Javascript显示错误和页面重新加载问题(Javascript display error and page reload problems)

所以这次我的项目是创建一个针对AI的井字游戏,它将跟踪玩家,计算机和绘图所赢得的游戏数量。 我的问题是,当计算机或玩家获胜或有平局时,它会显示[对象HTMLElement],然后每次显示1,而不是添加到赢/输/平局。

此外,当我只想清除棋盘时,开始新游戏会重新加载页面。 这也重置了我不想要的赢/输/抽奖次数。

https://jsfiddle.net/e0z632pz/

winCheck = function () {
if ((a1 == a2 && a1 == a3 && (a1 == "x")) ||
(b1 == b2 && b1 == b3 && (b1 == "x")) ||
(c1 == c2 && c1 == c3 && (c1 == "x")) ||
(a1 == b1 && a1 == c1 && (a1 == "x")) ||
(a2 == b2 && a2 == c2 && (a2 == "x")) ||
(a3 == b3 && a3 == c3 && (a3 == "x")) ||
(a1 == b2 && a1 == c3 && (a1 == "x")) ||
(a3 == b2 && a3 == c1 && (a3 == "x"))
) {
    xWin = true;
    winAlert();
    {
    playerWin = playerWin + 1;
    xWin = true;
    document.getElementById("playerWin").innerHTML = playerWin;
            }

我想,这尤其是问题所在。 修复一个将是完美的,因为我可以弄清楚其余的。

(注意:我有这个用于拖放,但我无法让它在jsfiddle上工作所以我把它改成了一个可点击的游戏,因为这不是我的问题,所以忽略图像框。)

(注2:我知道这可能很简单,但我已经醒了很久......哈哈)

谢谢!


So my project this time is to create a game of tic tac toe against AI that will track the number of games won by the player, computer, and draws. My problem is that when a computer or player wins, or there is a draw, instead of adding to the win/lose/draw, it displays [object HTMLElement] followed by a 1 every time.

Also, starting a new game reloads the page, when I just want to clear the board. This also resets the number of wins/loss/draw, which I do not want.

https://jsfiddle.net/e0z632pz/

winCheck = function () {
if ((a1 == a2 && a1 == a3 && (a1 == "x")) ||
(b1 == b2 && b1 == b3 && (b1 == "x")) ||
(c1 == c2 && c1 == c3 && (c1 == "x")) ||
(a1 == b1 && a1 == c1 && (a1 == "x")) ||
(a2 == b2 && a2 == c2 && (a2 == "x")) ||
(a3 == b3 && a3 == c3 && (a3 == "x")) ||
(a1 == b2 && a1 == c3 && (a1 == "x")) ||
(a3 == b2 && a3 == c1 && (a3 == "x"))
) {
    xWin = true;
    winAlert();
    {
    playerWin = playerWin + 1;
    xWin = true;
    document.getElementById("playerWin").innerHTML = playerWin;
            }

This particularly is where the issue lies, I assume. Fixing just one will be perfect, as I can figure out the rest.

(Note: I had this to work for a drag/drop, but I couldn't get it to work on jsfiddle so I changed it to a clickable game, because that isn't my issue, so ignore the box of images.)

(Note2: I know this is probably something simple, but I've been awake a long time.. haha)

Thanks!


原文:https://stackoverflow.com/questions/37692387
更新时间:2019-11-21 10:09

最满意答案

你有一个location.reload可以在新游戏中重新加载页面。 并且playerWin似乎没有在任何地方定义,也没有初始值。 也许宣布它并将其设置为0开始?

更新:

要将所有图像移回拖动框,您可以执行以下操作

$('.ximage').each(function(){
    $('.dragbox').append($(this));
});

您必须为图像添加ximage类。 现在他们把它作为id。 Id通常应该是唯一的。


You've got a location.reload that reloads the page on new game. And playerWin doesn't seem to be defined anywhere and doesn't have an initial value. Maybe declare it and set it to 0 to start with?

UPDATE:

To move all images back to the dragbox you'd do something like this

$('.ximage').each(function(){
    $('.dragbox').append($(this));
});

You'd have to add an ximage class to your images. Right now they have it as the id. Id is usually supposed to be unique.

2016-06-08

相关问答

更多

inf输出计算线斜率(inf output computing line slopes)

这一行: LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0])); 有一个除零误差。 我相信这个等式应该是: LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointA[0])); This line: LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0])); has a divide by zero error. I believ

OutputCache排除Mvc 4中的布局(OutputCache exclude Layout in Mvc 4)

甜甜圈缓存和甜甜圈洞做类似的事情,只是尝试这个例子http://www.dotnet-tricks.com/Tutorial/mvc/ODJa210113-Donut-Caching-and-Donut-Hole-Caching-with-Asp.Net-MVC -4.html Donut Caching and Donut Hole does similar thing, just try this example http://www.dotnet-tricks.com/Tutorial/m

Keras:flow_from_directory在fit_generator调用期间保存了太多图像(Keras: flow_from_directory saving too many images during fit_generator call)

fit_generator函数有两个额外的参数,可能是观察到的行为的原因: 默认值为=1意味着将使用额外的线程来加载图像。 线程将从提供的生成器中获取图像并将它们放入队列中。 max_queue_size ,默认值=10是此队列的最大长度。 当对fit_generator的调用返回时, fit_generator线程将停止,但在它继续用图像填充队列之前,即使其中一些不再需要,也会停止。 The fit_generator function has two additional parameter

访问属性Javascript的属性(Access property of a property Javascript)

这里有点小提琴 。 var indexes = [1, 3, 5]; var Obj_1 = {'100':[3,183,1],'200':[101,5,2,179],'300':[1,11]}; var Obj_2 = { }; var needle = 0; for (var i in indexes) { needle = indexes[i]; Obj_2['' + needle] = { }; for (var prop_1 in Obj_1) {

尝试将SQL中的值转换为Unix时间戳(Trying to convert values in SQL to Unix Timestamp)

在这里粘贴答案,因为我之前无法回答,因为我的帐户太新了。 好的语法错误最终是为UPDATE不喜欢的列字段添加引号。 删除那些给了我NULL值但查询运行。 然后我发现我必须在SELECT函数中包装时间戳转换。 这是我成功转换所有字段的代码: UPDATE wp_6222_postmeta SET meta_value = (SELECT UNIX_TIMESTAMP(STR_TO_DATE(meta_value`,'%m/%d/%Y'))) WHERE meta_key='wpcf-end-dat

无法调用未定义的方法'find'(Cannot call method 'find' of undefined)

选择器是什么并不重要,当您声明它时,您无法访问您声明的对象的属性。 为什么会被宣布? 您在opts对象上创建了属性ul 。 丑陋: 你可能想要的是: var opts = { ul: $(this).find('.carousel') } opts.li = opts.ul.find('li'); 但是如果你实际上不需要引用这两个组,那么: 清洁器: var opts = { li: $(this).find('.carousel li') } 同样好。 清洁

如何在React组件上测试定义为箭头函数(类属性)的组件方法?(How do I test component methods on a React component that are defined as arrow functions (class properties)?)

即使渲染很浅,也可以调用wrapper.instance()方法。 it("should call sort ids", () => { const wrapper = shallow(<Chat />); wrapper.instance().sortIds = jest.fn(); wrapper.update(); // Force re-rendering wrapper.instance().componentDidMount(); ex

找到与jQuery最近的链接(Find closest link with jQuery)

做这个: #main-about-div a { display: block } 由于你的LI元素每个只包含一个A元素,如果你使A元素成为块级别,它们应该占用它们的父LI元素的整个空间,然后单击LI元素将自动意味着相应的A元素是也点击了。 更新:看到您的CSS代码后。 #main-about-div li { list-style-type: none; font-size:13px; border-bottom:1px dotted #1f5779; } #

相关文章

更多

最新问答

更多
  • 根据Woocommerce中的自定义字段计算自定义购物车商品价格(Custom cart item price calculation based on dimentions custom fields in Woocommerce)
  • 分开foreach项目并单独打印(Separate foreach items and print them individually)
  • 写模式管道是否同步?(Is write mode pipe synchronous?)
  • 发现可用的Windows Phone强调色和本地化名称(Discover available Windows Phone accent colors and localized names)
  • 使用多线程和WPF更新集合(Update Collection with multithreading and WPF)
  • 如何在字符串的情况下使用同步?(How should the synchronization be used in case of strings?)
  • 打印唯一的行,比较不超过N个字符(Print unique lines, compare no more than N characters)
  • Javascript / jQuery - 如何调用switch case从另一个函数执行(Javascript/jQuery - how to call a switch case to execute from another function)
  • Hbase超时错误不断发生(Hbase timeout errors keep occuring)
  • 如何在Delphi中更改TabControl中活动TAB的颜色(How to change the color of active TAB in a TabControl, in Delphi)
  • 正则表达式:在sublime文本中替换一些PHP代码(Regex: replace some pieces of php code in sublime text)
  • 在带有Scene2D的LibGDX中,如何在按下按钮时连续向右走?(In LibGDX with Scene2D, how can I continuously walk to the right when a button is pressed?)
  • 累加器如何在Haskell中工作?(How do accumulators work in Haskell?)
  • 使用开关检查市场和前缀与正确的货币符号(using switch to check market and prefix with correct currency symbol)
  • 在哪里分配一次使用类?(Where to allocate one time use class?)
  • 如何从两个DateTime / NaiveDateTime获取持续时间?(How do I get Duration from two DateTime / NaiveDateTime?)
  • 解析TimeSpan大于24小时?(Parse a TimeSpan greater than 24 hours? [duplicate])
  • 如何在球拍中本地更改阅读规则?(How to locally change reading rules in racket?)
  • 数据库应该由DI注入时的模拟存储(通过构造函数)(Mock storage when database should be injected by DI (through constructor))
  • 使用maven集成2个eclipse项目(Integrate 2 eclipse projects using maven)
  • 角度ng-repeat不检测变化(Angular ng-repeat not detecting changes)
  • Xaml组件在Silverlight + XNA应用程序的多个页面中可见(Xaml component visible in multiple pages in Silverlight+XNA applications)
  • 如何将`var`变量等同于另一个查询(How to equate `var` variable to another query)
  • 如何设计hyperledger链代码以适合您的业务?(how to design your chaincode of hyperledger to fit for your business?)
  • PHP readfile错误(PHP readfile error)
  • 在Date之后排序列表然后是时间(Sorting list after Date then time)
  • Android内部版本号(Android build number)
  • 在没有预设退出条件的情况下停止无限循环(stopping an infinite loop with no preset exit condition)
  • Phonegap应用程序全屏通过html页面中的按钮(Phonegap Application fullscreen through a button in html page)
  • PAA是否适合在门户网站中自动执行wcm库部署和设置?(Is PAA a good candidate for automating wcm library deployment and setup in portal?)