image_0とimage_1の2つのイメージがあり、クリックするたびにそのイメージのIDを示すアラートを表示します。これを行うために、私はこれらの関数を格納する配列を作成しました(以前の私の問題のために必要でした:https://stackoverflow.com/questions/41003122/looping-in-jquery-only-remembers-last-iteration?noredirect=1#comment69215730_41003122)。img.click()関数の配列
以下のコードは私の試みです。しかし、いずれかの画像をクリックすると何も起こりません。どうして?
HTML:
<img id="image_0" src="http://placehold.it/350x150" width="300">
<img id="image_1" src="http://placehold.it/350x150" width="300">
Javascriptを:CreateImageClickFunction(image_id)
として
$(document).ready(function()
{
// The number of images shown
num_images = 2
// List of functions for each thumbnail click.
var image_click_functions = new Array(num_images);
// Define the function for when the thumbnail is clicked
function CreateImageClickFunction(image_id)
{
return function() { alert(image_id) };
}
// Loop through all images, and define the click functions
for (i = 0; i < num_images; i++)
{
image_click_functions[i] = CreateImageClickFunction(i);
image_id = "#image_" + i;
$(image_id).click(function()
{
image_click_functions[i];
});
}
});
これは逆です。ワンクリック機能があり、クリックした要素から情報を引き出します。 –