Jquery:在.append中传递ID
Jquery: Passing ID inside .append
这里的新手收到了一个问题,希望它足够简单:
所以我有这行代码;
$("#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'));
提前感谢
您可以将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/
如果您有兴趣传递父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 */
};
});
您可以通过:
$("#r"+i+"c"+j).append($("<img id='"+yourID+"'>", {style:"display:block; margin: 0 auto;", src: imagefile, width: cellDimension + "px", height: cellDimension + "px"}));
如果我正确理解你,我会这样做:
$("#r" + i + "c" + j).append($("<img>", {
style: "display:block; margin: 0 auto;",
src: imagefile,
width: cellDimension + "px",
height: cellDimension + "px",
id: id
}));
以下是工作代码:http://jsfiddle.net/silkster/jqqLmm46/
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- difference between '$(<%= DDL.ID %>) & $('
- 使用Javascript获取所选选项ID
- 根据id将json数组组合为一个json数组
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 对id以某个字符串开头的元素进行计数
- 如果元素's的ID以数字开头
- 通过id和class属性获取元素
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 使用当前日期生成随机id
- Javascript-ID冲突的几率
- Href:当前DIV中的目标ID
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- 如何在 append() 上添加具有唯一 id 的编辑按钮
- 无法在 jquery .append() 方法中的按钮中添加类或 id 以制作有效的按钮
- 如何在jquery中的append函数中为html标签生成变量id
- Jquery:在.append中传递ID
- JQuery.append() -如何在没有ID的情况下进行追加
- 在我将其与$("#id")一起使用后,相同的代码就不起作用了.append.代码看起来像没有加载jq
- 使用Javascript将.insertAfter或.append添加到元素Id.它's是一个脚本标记,因此它可