我想添加一个成功和失败的功能,但我该怎么办呢?我有以下代码:
class ={};
class.hey = function(values)
{
document.getElementById(values.id).innerHTML = values.Val ;
return class;
}
class.hey({
id:"target",
Val: "hello world",//this wil work and will display that value in the span tag
})
<span id="target"></span>//here will appear the value passed before
我怎么能添加一个功能,告诉我一切是好还是错?我一直在尝试,但我无法解决这个问题 id如何工作的示例
class ={};
class.hey = function(values)
{
document.getElementById(values.id).innerHTML = values.Val ;
return class;
}
class.hey({
id:"objetive",//the element does not exist
Val: "hello world",//this wil work and will display that value in the span tag
success:function(msg)
{
alert("ok!")
},
error:function(msg){
alert("wrong");
}
})
<span id="target"></span>//here will appear the value passed before
答案 0 :(得分:3)
这根本不是JavaScript自动完成的,但你可以这样做:
class.hey = function (value) {
var value = document.getElementById(values.id);
if (!value) {
value.error(values.id + " is not an element")
return false;
}
value.innerHTML = values.val;
value.success("okay!");
}