如何将Div的id作为参数传递给函数
How to pass the id of Div as an argument to a function
我有一个div,只需使用jQuery单击按钮即可动态添加到页面中。
我希望能够使用RemoveArtist()
功能点击交叉按钮.png来删除Div。
$("<div class='"input-append'" id='"artist"
+ artists.length - 1 + "'" ><label style='"background-color:#e0ffff'">"
+ artistVal + "</label><img onclick='"RemoveArtist()'" id='"removeartist'""
+ " src='"/Content/bootstrap/img/cross-button.png'" /></div>")
.appendTo(addDiv);
移除艺术家功能应该如下所示:
var RemoveArtist = function (div,artistlength) {
$('div').remove();
artists.splice(artistlength, 1);
};
如何将实际的div传递给RemoveArtist
函数,以及作为div的artists.length - 1
值的artistlength
?
所以基本上,我试图删除Div,并从艺术家数组中删除艺术家。
在我看来,最好创建实际元素而不是字符串
这是更多的东西要写,但使每一件事都更容易跟踪:
var input_append = $('<div />', {id: 'artist' + (artists.length - 1)}),
label = $('<label />', {style: 'background-color:#e0ffff',
text : artistVal
}
),
image = $('<img />', {id: 'removeartist',
src: '/Content/bootstrap/img/cross-button.png',
on: {
click: function() {
input_append.remove();
artists.splice(artists.length - 1, 1);
}
}
}
);
addDiv.append( input_append.append(label, img) );
相关文章:
- 函数未将值作为参数传递
- 如何在HTML元素上创建函数,而不是将元素作为参数传递
- 如何传递函数中的参数
- 将参数传递给函数
- 如何向onClick事件处理程序传递一个接受参数的函数,并且仍然将该函数绑定到组件's”;这个“;上下文
- 传递带有参数的函数
- javascript,将参数传递给函数内部的闭包中的回调
- 将字符串作为参数传递给函数onclick event jquery
- 将修改后的数组作为参数传递给函数
- 代码气味-将布尔控制参数传递给函数
- 将参数传递给函数,同时保留事件处理程序
- 如何使用按钮将参数传递给函数
- 作为参数传递时如何计算函数
- 将对象作为参数传递时的 Javascript 函数作用域
- 将回调作为参数传递给函数
- 在构造控制器、服务等时作为函数参数传递之前列出的角度 JS 变量
- 作为参数传递的函数的异步执行
- 将请求作为参数传递给函数
- 什么时候需要在javascript中的函数中将对象作为参数传递
- Javascript,对象,用参数传递函数作为参数- angular, jquery概念基本误区