gpt4 book ai didi

jquery - RaphaelJS 在点击时删除元素

转载 作者:行者123 更新时间:2023-12-01 08:03:21 25 4
gpt4 key购买 nike

如果删除单选按钮为“true”,我想在单击形状时删除该形状。现在我只能删除整篇拉斐尔论文。有人可以帮助我吗?我使用 RaphaelJS 和 Jquery。

HTML

<html lang="en" style="height: 100%;">
<head>
<script src="raphael-min.js" type="text/javascript"></script>
<script src="jquery-min.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
</head>
<body>
<input type='radio' id='un' name="action" value="un">draw un</button>
<input type='radio' id='deux' name="action" value="deux">draw deux</button>
<input type="radio" id='remove' name="action" value="remove"> remove


<div id="holder">
</div>

</body>
</html>

JS

$(document).ready(function() {
var paper = Raphael('holder', '800', '600');
var mySet = paper.set();

var tojsonplease = [
{
category: 'playground',
type: "rect",
x: 10,
y: 10,
width: 600,
height: 300,
fill: "#eee"
}];
paper.add(tojsonplease);

addme = function(cat, myX, myY) {
switch (cat)
{
case '1' :
specs = {
category: '1',
type: 'rect',
x: myX,
y: myY,
width: 20,
height: 20,
fill: 'yellow'
};
break;
case '2':
specs = {
category: '2',
type: 'rect',
x: myX,
y: myY,
width: 10,
height: 20,
fill: 'red'
};
break;
}
var newElement = new Array();
newElement[0] = specs;
tojsonplease.push(specs);
paper.add(newElement);
console.log(tojsonplease);
};

$('#holder').click(function () {


if ($("#remove").is(":checked")) {
paper.remove();
};
if ($("#deux").is(":checked")) {
SX = event.pageX - $(document).scrollLeft() - $('#holder').offset().left;
SY = event.pageY - $(document).scrollTop() - $('#holder').offset().top;
addme('1', SX, SY);
};
if ($("#un").is(":checked")) {
SX = event.pageX - $(document).scrollLeft() - $('#holder').offset().left;
SY = event.pageY - $(document).scrollTop() - $('#holder').offset().top;
addme('2', SX, SY);
};


});
});

最佳答案

通常,您可以使用 element.remove() 删除形状:

var element = paper.rect(100,100,100,100);
element.remove() // this removes the element

不要这样做:

if ($("#remove").is(":checked")) 
paper.remove();

这将删除所有内容,只需删除如上面所示的元素。

您应该为您的元素分配ids,这将帮助您使用Paper.getById(id)函数。您将能够获得需要删除的确切元素。

<强> LOOK AT THE WORKING DEMO

var paper = Raphael("holder",500,500);
var which_one_to_remove = -1;

// use data() to assign ids that correspond to the order
// in which you push() the elements into the array object
var c1 = paper.circle(100, 100, 80).attr({fill:'red'}).data('id', '0');
var c2 = paper.circle(200, 200, 80).attr({fill:'yellow'}).data('id', '1');
var c3 = paper.circle(300, 300, 80).attr({fill:'blue'}).data('id', '2');

var elements = [];
elements.push(c1);
elements.push(c2);
elements.push(c3);

c1.click(function()
{
which_one_to_remove = this.data('id');
});
c2.click(function()
{
which_one_to_remove = this.data('id');
});
c3.click(function()
{
which_one_to_remove = this.data('id');
});

$('#holder').click(function () {
if ($("#remove").is(":checked"))
{
elements[which_one_to_remove].remove();
};
});

关于jquery - RaphaelJS 在点击时删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18119232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com