如何添加成功功能?

How can I add a success function?

本文关键字:成功 功能 添加 何添加      更新时间:2023-09-26

我想添加一个成功和失败的功能,但我怎么能做到呢?我有下面的代码:

    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 like如何工作的例子

    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

这不是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!");
}