I have created a dynamic div, along with the table...I want that when i click the image inside the div, it will give the the id of the div on which i have clicked...below is the code. Please any one point out where the mistake is...!? In the alert box i am unable to get the id of the div
我已经创建了一个动态div,以及表...我希望当我单击div中的图像时,它将给出我点击的div的id ...下面是代码。请任何人指出错误在哪里......!?在警告框中,我无法获得div的ID
following code creates the div
以下代码创建div
function create_newGrid() {
//create div for corresponding table
var tableDiv = $('<div/>').attr({ "id": "settingsDiv-tab" + nooftables });
var divImage = $('<img/>').attr({ "src": "settingsImage/settings.png", "onclick": "openTableSettings(this);" })
$(tableDiv).append(divImage);
// append grid to designer
$(document.getElementById("center")).append(tableDiv);
$(document.getElementById("center")).append(table);
}
following is the function that executes when a image in the div is clicked
以下是单击div中的图像时执行的功能
function openTableSettings(div) {
alert($(div).attr("id"));
}
4 个解决方案
#1
1
You are passing a reference to the img to openTableSettings()
. Use .closest()
to get the div:
您正在将对img的引用传递给openTableSettings()。使用.closest()来获取div:
function openTableSettings(img) {
alert($(img).closest("div").attr("id"));
}
#2
1
The problem is that you have the onclick on the image, which doesn't have an ID. If you run this in your browser console, it will add a div with text to the bottom of this page and you can see the id being retrieved properly. (your code but adapted)
问题是您在图像上有onclick,它没有ID。如果您在浏览器控制台中运行它,它将在此页面的底部添加带文本的div,您可以看到正确检索的ID。 (你的代码,但改编)
//create div for corresponding table
var tableDiv = $('<div/>').attr({ "id": "settingsDiv-tab", "onclick": "alert($(this).attr('id'));"});
var divImage = $('<p>this is a big bunch of text</p>');
tableDiv.append(divImage);
$(document.body).append(tableDiv);
#3
0
Maybe it's better if you do
也许你做得更好
var divImage = $('<img/>').attr({ "src": "settingsImage/settings.png", class: "clickable"});
and then
接着
$(window).on('click', 'img.clickable', function(){
alert(this.id);
});
(this assumes jQuery >= 1.7)
(这假定jQuery> = 1.7)
#4
0
try this:
尝试这个:
$("div > img").click(function() {
id = $(this).parent("div").attr("id");
alert(id);
})
#1
1
You are passing a reference to the img to openTableSettings()
. Use .closest()
to get the div:
您正在将对img的引用传递给openTableSettings()。使用.closest()来获取div:
function openTableSettings(img) {
alert($(img).closest("div").attr("id"));
}
#2
1
The problem is that you have the onclick on the image, which doesn't have an ID. If you run this in your browser console, it will add a div with text to the bottom of this page and you can see the id being retrieved properly. (your code but adapted)
问题是您在图像上有onclick,它没有ID。如果您在浏览器控制台中运行它,它将在此页面的底部添加带文本的div,您可以看到正确检索的ID。 (你的代码,但改编)
//create div for corresponding table
var tableDiv = $('<div/>').attr({ "id": "settingsDiv-tab", "onclick": "alert($(this).attr('id'));"});
var divImage = $('<p>this is a big bunch of text</p>');
tableDiv.append(divImage);
$(document.body).append(tableDiv);
#3
0
Maybe it's better if you do
也许你做得更好
var divImage = $('<img/>').attr({ "src": "settingsImage/settings.png", class: "clickable"});
and then
接着
$(window).on('click', 'img.clickable', function(){
alert(this.id);
});
(this assumes jQuery >= 1.7)
(这假定jQuery> = 1.7)
#4
0
try this:
尝试这个:
$("div > img").click(function() {
id = $(this).parent("div").attr("id");
alert(id);
})