onclick()で関数を呼び出す– JavaScript?
次がボタンだとしましょう-
<button onclick="displayingMessageOnButtonClick()">Press Me</button>
上記のボタンをクリックするだけで関数を呼び出す必要があります。
例
以下はコードです-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<body>
<p id="showTheTextMessage"></p>
<button onclick="displayingMessageOnButtonClick()">Press Me</button>
</body>
<script>
function displayingMessageOnButtonClick() {
const showMessage = document.getElementById('showTheTextMessage');
showMessage.innerHTML = 'Welcome to Javascript Program...';
showMessage.style.display = 'block';
}
</script>
</html> 上記のプログラムを実行するには、ファイル名anyName.html(index.html)を保存します。ファイルを右クリックし、VSCodeEditorで[ライブサーバーで開く]オプションを選択します-
出力
出力は次のとおりです-
次に、ボタンを押します。以下は出力です-
-
JavaScriptで変数に関数を割り当てますか?
returnステートメントを使用して、関数を変数に割り当てます。以下はコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel=&quo
-
Tkinterのボタンまたはキーを使用して関数を呼び出す
特定のアプリケーションでボタンまたはキーが押されるたびに関数を呼び出したいと仮定します。 bind( 、 callback_function を使用して、ボタンまたはキーで操作を含む関数をバインドできます。 ) 方法。ここでは、呼び出す必要のあるイベントまたは関数に任意のキーをバインドできます。 例 この例では、ボタンをクリックするたびにダイアログボックスを開く関数を作成しました。 #Import the required libraries from tkinter import * from tkinter import ttk from tkinter import messagebo