在js中捕获null对象
catch null object in js
如何捕捉对象为null或不是对象。事实上,我已经写了这行如果条件解决它,但没有工作。
但是生成错误:-"object is null or not a object"
var preWynum = "";
function paint(Wynum)
{
// alert("Wynum:"+Wynum +", preWynum:"+preWynum);
if(document.getElementById(preWynum) && document.getElementById(Wynum))
{
document.getElementById(preWynum).style.backgroundColor='white';
document.getElementById(Wynum).style.backgroundColor='yellow';
}
preWynum = Wynum;
}
我不相信它为什么不运行。还有其他想法吗?
preWynum和Wynum是tr(表行)的id。
我想将背景颜色设置为黄色到当前选定的行(id为Wynum)。
错误是因为您正在将一个对象(不存在)传递给getElementById()
,而它需要一个字符串:
// Put quotes around 'preWynum' and 'Wynum'
if(document.getElementById('preWynum') && document.getElementById('Wynum'))
{
document.getElementById('preWynum').style.backgroundColor='white';
document.getElementById('Wynum').style.backgroundColor='yellow';
}
相关文章:
- 谷歌地图API-“;toGeoJson”;返回几何体为null的对象
- 验证对象是否为null Javascript/Angularjs
- 对象为null或IE9中存在未定义错误
- 如何计算一个对象中五个属性中有多少是非null的
- Microsoft JScript运行时错误:无法获取属性'的值;样式':对象为null或未定义
- javascript对象显示为null,但object.properties返回数据
- global.getElementById('..')为null或不是对象
- IE8错误:为null或不是对象,但仍然可以跟踪/控制台注销
- AJAX请求中的JSON对象为Null或空
- Highcharts无法设置属性'的值;要点':对象为null或未定义
- ui.selectmenu投掷'this.newelement.0'为null或不是对象
- 添加到序列化数组中的复杂对象在服务器端始终为 null
- 什么是 ClojureScript 类似地将不存在的对象值设置为 JavaScript 中的 null
- 最大查询返回带有sequelise.js的null对象
- 从JSON对象中删除jQuery和null对象
- 从客户端到服务器的SocketIO回调没有'不起作用(改为null对象)
- 迭代react元素中的undefined/null对象
- “Interiting"From null对象似乎不会影响性能
- Null对象中属性默认值的赋值
- 在js中捕获null对象