Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery:在内部传递ID。追加_Javascript_Jquery_Function_Selector_Identity - Fatal编程技术网

Javascript Jquery:在内部传递ID。追加

Javascript Jquery:在内部传递ID。追加,javascript,jquery,function,selector,identity,Javascript,Jquery,Function,Selector,Identity,这里的新手,有一个问题希望足够简单: 所以我有这行代码 $("#r"+i+"c"+j).append($("<img>", {style:"display:block; margin: 0 auto;", src: imagefile, width: cellDimension + "px", height: cellDimension + "px"})); $(“#r”+i+“c”+j).附加($) 我需要传递“”一个ID,这样我就可以在单独的函数中与它交

这里的新手,有一个问题希望足够简单:

所以我有这行代码

            $("#r"+i+"c"+j).append($("<img>", {style:"display:block; margin: 0 auto;", src: imagefile, width: cellDimension + "px", height: cellDimension + "px"}));
$(“#r”+i+“c”+j).附加($)
我需要传递“”一个ID,这样我就可以在单独的函数中与它交互。我尝试了以下方法:

            $("#"+ID+"<img>",
$(“#”+ID+”

提前感谢

您可以在创建图像时将
id
作为传递对象的属性传递

            $("#r"+i+"c"+j).append($("<img>", {style:"display:block; margin: 0 auto;", src: imagefile, width: cellDimension + "px", height: cellDimension + "px"}).attr('ID'));
演示:

您可以通过以下方式完成此操作:

$("<img>", {
    style:"display:block; margin: 0 auto;",
    src: imagefile,
    width: cellDimension + "px",
    height: cellDimension + "px"}
).attr('id', ID_HERE); // again, string variable or literal

$(“#r”+i+“c”+j).追加($)(“如果我理解正确,我会这样做:

    $("#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).附加($)

下面是工作代码:

如果您希望传递父ID,以便它可以成为图像ID的一部分,下面是如何操作:

$("#r" + i + "c" + j).append($("<img>", {
    style: "display:block; margin: 0 auto;",
    src: imagefile,
    width: cellDimension + "px",
    height: cellDimension + "px",
    id: id 
}));
$(“#r”+i+“c”+j).append(function(){
返回$(“”){
//要设置的任何属性,例如:
类:“新图像”,
“数据id”:this.id,
id:this.id+'-something'/*因为id必须是唯一的*/
};
});

你有兴趣通过
“#r”+i+“c”吗+
新图像的值,还是要给图像一个新ID?此行是向div添加图像的循环的一部分。我需要在单独的函数中与这些图像交互,以便可以移动它们,因此我需要一种单独选择图像的方法。添加“#r”+I+“c”有什么好处吗+j作为ID而不是任何其他名称/数字?这完全取决于您希望使用ID做什么。如果您希望能够选择任何特定的一个图像,那么请务必使用ID。但是,您必须记住,没有两个元素具有相同的ID;ID应该是唯一的。我建议使用css类和数据属性(如果需要)sary.你能提供一个演示,让我们更好地了解你所描述的内容吗?我之所以需要所有图像文件的ID,是因为它们是棋盘上的棋盘格,我需要四处移动。我想一个可拖动/可拖动的函数就可以了,因此我需要能够选择每个图像文件(棋子)另外,这里有一个指向所有文件的链接,我使用的是lab2-index.html。其余的由我们的老师提供。代码中有一些外来词(英语不是我的母语)。Bildefil=imageFile,还有任何注释文本都可以直接编辑谢谢,你的回答非常有用!
$("#r" + i + "c" + j).append($("<img>", {
    style: "display:block; margin: 0 auto;",
    src: imagefile,
    width: cellDimension + "px",
    height: cellDimension + "px",
    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 */ 
    };
});