在Javascript函数中传递对象
Passing objects in Javascript function
不确定我是否在以下javascript代码中获得了正确的对象:
<form>
What is 5 + 5?: <input type ="number" id ="num_answer;"/>
</form>
<script>
function basic_math(){
var num_val = document.getElementById('num_answer').value;
if (num_val == 10) {
document.getElementById('num_answer').style.backgroundColor = green;
}
else{
document.getElementById('num_answer').style.backgroundColor = red;
}
}
</script>
<button type = "button" onclick ="basic_math();"> Solve!
</button>
我得到的错误:
Uncaught TypeError: Cannot read property 'value' of null
应该是:
<input type ="number" id="num_answer"/>
(无分号)
去掉那个分号:
What is 5 + 5?: <input type ="number" id ="num_answer;"/>
<!-- This is a typo ^ -->
这里有一个使用JQuery 的解决方案
<p> <span> What is 5 + 5? </span>
<input type="number" id="num_answer" /> </p>
function basic_math()
{
num_val = $("#num_answer").val();
if (num_val == 10) {
$('#num_answer').css("color", "white");
$('#num_answer').css("background-color", "green");
} else {
$('#num_answer').css("background-color", "red");
}
}
http://jsfiddle.net/fonsecat/WMJh3/17/
相关文章:
- 面向对象JavaScript中的私有函数
- 对象 Javascript 中的标签无效 - 想要添加事件列表器
- 将字符串转换为对象 javascript/jquery
- 正在检查对象javascript中是否存在嵌套属性
- 时间-日期对象JavaScript getUTCMilliseconds
- 仅在对象(javascript)中解析值
- 使用对象(JavaScript或jQuery)填充选择下拉列表
- 引用另一个对象javascript中的对象
- 如何删除列表中的对象?Javascript nodejs和下划线
- 无法从日期对象javascript获取日期和月份
- 如何在if语句中使用对象-Javascript
- 对象javascript错误
- 对象Javascript的少数实例
- 位置对象Javascript
- 将字符串传递到对象javascript中
- 获取对象Javascript或jQuery的最后一个值
- 鼠标接近对象Javascript
- 这个mixins代码是书中的错误吗;面向对象JavaScript的原理”;
- 使用闭包编译器编写更好的面向对象JavaScript完整示例代码
- 访问对象javascript数组中的对象属性值