Web/javascript / jQuery
[jQuery] HTML에서 onClick으로 jQuery 호출방법
RAY.D
2015. 4. 28. 08:52
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>