- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 javascript 新手,正在尝试制作一个每次单击都会循环打开/关闭/自动的按钮。每个状态还必须运行 if 语句中的代码和 switch 中的代码。我目前无法让它正常运行。稍后,一旦我开始工作,我希望“if 语句”函数位于其自己的 .js 文件中,以便我可以在其他开/关/自动按钮中引用它。而开关将是主代码的一部分。我做错了什么?
function cycle()
{
var onoffB = ();
if (document.getElementById("button1").value="On")
{
onoffB=1;
document.getElementById("button1").value="Off";
}
else if (document.getElementById("button1").value="Off")
{
onoffB=2;
document.getElementById("button1").value="Auto"
}
else
{
onoffB=0;
document.getElementById("button1").value="On"
}
switch(onoffB)
{
case 0:
//running code;
break;
case 1:
//running code;
break;
case 2:
//running code;
break;
}
}
</script>
</head>
<body>
<input type="button" id="button1" value="On" onclick="cycle()">
</body>
</html>
最佳答案
嗯,我立刻就发现了两个大问题。首先,以下语法是无效的 JavaScript:
var onoffB = ();
我认为你的意思是 onoffB
有一个未定义的状态,你最好这样做:
var onoffB = null;
其次,您使用的是赋值 (=
),而不是比较(==
或 ===
)。 (根据经验,您应该始终更喜欢 ===
而不是 ==
,除非您有充分的理由并且知道自己在做什么。)请考虑以下内容:
var x = 3; // *sets* the value of x to 3
var x == 3; // invalid syntax
var x === 3; // invalid syntax
if( x = 2 ) { print 'x is 2'; } // will *always* print 'x is 2', no matter what
// the value of x is; x now has the value 2
// PROBABLY NOT WHAT YOU WANT
if( x = 0 ) { print 'x is 0'; } // will *never* print 'x is 0', no matter what
// the value of x is; x now has the value 0
// PROBABLY NOT WHAT YOU WANT
if( x === 5 ) { print 'x is 5'; } // will only print 'x is 5' if the value of x
// is 5; value of x
if( x === 0 ) { print 'x is 0'; } // will only print 'x is 0' if the value of x
// is 0; value of x unchanged
我也会对此做出很多风格上的改变。由于您的格式选择,您的代码确实很难阅读。此外,还有很多不必要的重复代码。如果我写这篇文章,我会将其缩短为以下内容:
switch( document.getElementById("button1").value ) {
case 'On':
// do stuff
break;
case 'Off':
// do stuff
break;
case 'Auto':
// do stuff
break;
default:
// probably an error condition; show message, maybe?
break;
}
如果您确实需要开/关/自动按钮的值,可以将其设置在case
主体内。
关于javascript - 尝试制作一个开/关/自动按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19203245/
如何使用 ToggleButton 启用/禁用推送通知 示例: ToggleButton 禁用 (OFF) >>> 推送通知应该停止 ToggleButton 启用 (ON) >>> 推送通知 Sho
我有一个 div,它通过简单的转换将自身转换为: div{ transform: translate3d(0, -100%, 0); transition: all .5s; } div.ac
我尝试为静音/取消静音按钮创建一个开/关按钮: override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
我正在手动设置 Jest 。 我的 repo 结构: my-proj - src - components ... - accordion - index.jsx - t
我有一个这样的测试失败了,因为没有调用模拟,问题是模拟被调用但在测试完成之前。 test('should submit if proper values', () => { const spy =
目前我正在使用标准的 testRegex 逻辑来运行我的测试 "jest": { "moduleFileExtensions": [ "ts", "js"
目前我有这个测试: import toHoursMinutes from '../../../app/utils/toHoursMinutes'; describe('app.utils.toHour
使用Chai,您可以创建一个 spy 对象,如下所示: chai.spy.object([ 'push', 'pop' ]); 使用 Jasmine ,您可以使用: jasmine.createSpy
我正在编写一个 Jest 测试,其中我调用一个函数并期望返回一个对象,如下所示: const repository = container => { const makeBooking = (us
当我单独运行每个测试时,它们都成功了。但是当我通过 npm test 一起运行它们时第二个测试失败: Expected number of calls: 2 Received number of ca
我们最近将两个不同的 repos 迁移到一个 monorepo 中。每个都使用 jest 和自己的自定义配置,在他们自己的 package.json 文件中定义。 我想使用 --projects标志以
我试图模拟属性(property) tz和一个使用 jest 的函数,但我不知道将这两个东西一起模拟: 如果运行类似: jest.mock('moment-timezone', () => () =>
我正在尝试设置 Jest 来测试我的应用程序的发展。我收到以下错误: SyntaxError: Unexpected identifier > 1 | const screenSize = requi
我将 Jest 与 React-Native 结合使用,并且偶然发现了一个问题。 App.js 组件中的一小段代码导致 50:50 分支覆盖率: const storeMiddleware = __D
我在下面创建了一个 Jest 测试文件。但是没有创建该文件的快照。我的代码有什么问题? import React from 'react'; import Carousel from './compo
我正在尝试弄清楚如何更新单个快照文件。在文档中,它说只需添加 -t 并且我假设文件名,但这对我不起作用。 例如,在我使用的终端中。 jest -u -t test/js/tests/component
我是 JEST 新手,目前正在测试一个 Javascript 组件,该组件在其 onComponentDidMount 中进行 API 调用。根据 ajax 调用(api 调用)的返回数据,我的组件显
我正在尝试开玩笑地为我的 Web 组件项目编写测试。我已经在 es2015 预设中使用了 babel。我在加载 js 文件时遇到问题。我遵循了一段代码,其中 document对象有一个 current
我刚刚开始使用 jest,但有些事情我不太清楚。 例如,为什么要测试此功能: const liElement = object => `${object.title}`; 与: expect(liEl
我正在编写需要定义 window.location.href 的单元测试第一个单元测试创建如下 describe('myMethod()', () => { beforeEach(()
我是一名优秀的程序员,十分优秀!