当用户双击画布时,我想添加一个数组' tasks',当用户双击生成时,我想将其从数组中删除。这是我的实施:
<script type="text/javascript">
jsPlumb.ready(function() {
jsPlumb.Defaults.Container = $('#container');
var tasks = []
var i = 0;
$('#container').dblclick(function(e) {
var stateId = 'state' + i;
var newState = $('<div>').attr('id', stateId).addClass('item');
var title = $('<div>').addClass('title')
var stateName = $('<input>').attr('type', 'text')
title.append(stateName)
stateName.keyup(function(e) {
if (e.keyCode === 13) {
$(this).parent().text(this.value)
}
})
stateName.focus();
var connect = $('<div>').addClass('connect');
newState.css({
'top': e.pageY,
'left': e.pageX
});
newState.append(title);
newState.append(connect);
$('#container').append(newState);
jsPlumb.makeTarget(newState, {
anchor: 'Continuous'
});
jsPlumb.makeSource(connect, {
parent: newState,
anchor: 'Continuous'
});
jsPlumb.draggable(newState, {
containment: 'parent'
});
newState.dblclick(function(e) {
console.log(tasks)
console.log(this)
var index = tasks.indexOf(this)
alert(index); //ERROR, ALWAYS -1
// if (index > -1) {
// tasks.splice(index, 1)
// }
jsPlumb.detachAllConnections($(this));
$(this).remove();
e.stopPropagation();
});
tasks.push(newState)
// alert(tasks)
i++;
});
});
</script>
问题在于这一行:
var index = tasks.indexOf(this)
alert(index); //ERROR, ALWAYS -1
答案 0 :(得分:0)
试试这个:
newState.dblclick(function(e) {
var index = tasks.indexOf(this)
if (index > -1) {
tasks.splice(index, 1)
}
jsPlumb.detachAllConnections($(this));
$(this).remove();
e.stopPropagation();
});
tasks.push(newState[0])
i++;
newState [0]应该是您正在寻找的dom元素。 newState是包装器jquery对象