新手在这里,有一个问题,希望是足够简单的:
所以我有这行代码;
$("#r"+i+"c"+j).append($("<img>", {style:"display:block; margin: 0 auto;", src: imagefile, width: cellDimension + "px", height: cellDimension + "px"}));
我需要向“< img >”传递一个ID,这样我就可以在一个单独的函数中与它交互。我尝试过以下几种方法:
$("#"+ID+"<img>",
和
$("#r"+i+"c"+j).append($("<img>", {style:"display:block; margin: 0 auto;", src: imagefile, width: cellDimension + "px", height: cellDimension + "px"}).attr('ID'));
提前感谢
发布于 2014-10-24 09:02:34
您可以将id
作为创建图像时传递的对象的属性进行传递。
$("<img>", {
style:"display:block; margin: 0 auto;",
src: imagefile,
width: cellDimension + "px",
height: cellDimension + "px",
id: ID_HERE } // ID here - either a string variable, or string literal like "imageId"
);
演示:http://jsfiddle.net/dmpd9a64/
此外,您还可以使用attr
传递一个值,如下所示:
$("<img>", {
style:"display:block; margin: 0 auto;",
src: imagefile,
width: cellDimension + "px",
height: cellDimension + "px"}
).attr('id', ID_HERE); // again, string variable or literal
演示:http://jsfiddle.net/dmpd9a64/1/
发布于 2014-10-24 09:32:45
如果您有兴趣传递父ID,以便它可以成为图像ID的一部分,下面是如何完成此操作的方法:
$("#r" + i + "c" + j).append( function() {
return $("<img>", {
//any attributes you'd like to set, such as:
class: 'new-image',
'data-id': this.id,
id: this.id + '-something' /* since IDs must be unique */
};
});
发布于 2014-10-24 09:05:14
您可以通过以下方式达到此目的:
$("#r"+i+"c"+j).append($("<img id='"+yourID+"'>", {style:"display:block; margin: 0 auto;", src: imagefile, width: cellDimension + "px", height: cellDimension + "px"}));
https://stackoverflow.com/questions/26539894
复制相似问题