2013-03-02 4 views
2

성공과 실패 함수를 추가하고 싶습니다. 어떻게 할 수 있습니까? 이 코드는 다음과 같습니다.어떻게 성공 기능을 추가 할 수 있습니까?

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 

모든 것이 잘되었거나 잘못되었는지를 알려주는 함수를 어떻게 추가 할 수 있습니까? 필자는 노력하지만 난이 전혀 자바 스크립트에 자동으로 일이 아니다

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

jQuery없이 jQuery를 할 수 있습니까? – adeneo

+0

jajaja 너는 그것을 얻었다 – Misters

답변

3

같은 ID가 작동하는 방법이 예를 해결할 수없는,하지만 당신은 그것을 할 수 있습니다 : 당신이하려고

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!"); 
} 
+0

OMG 그것은 일했다 !!!! – Misters

관련 문제