[jQuery] HTML에서 onClick으로 jQuery 호출방법

Posted by RAY.D
2015. 4. 28. 08:52 Web/javascript / jQuery
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.




Calling jQuery method from onClick attribute in HTML


jQuery 에서 HTML태그안에 onClick 이벤트로 파라미터 넘기기 1




jQuery Function 은 이렇게


function showMessage(msg) {
   alert(msg);
};



HTML 은 요렇게


<html>
<head>
 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 
<script language="javascript">
    
// define the function within the global scope
    $.fn.
MessageBox = function(msg) {
        alert(msg);
    };

// or, if you want to encapsulate variables within the plugin
    (
function($) {
        $.fn.
MessageBoxScoped = function(msg) {
            alert(msg);
        };
    })(jQuery); 
//<-- make sure you pass jQuery into the $ parameter
 
</script>
</head>
<body>
 
<div class="Title">Welcome!</div>
 
<input type="button" value="ahaha" id="test" onClick="$(this).MessageBox('msg');" />
</body>
</html>