Events 为什么在拉斐尔中更改文本会取消事件?

Events 为什么在拉斐尔中更改文本会取消事件?,events,raphael,Events,Raphael,我有一个Raphael文本元素,它具有mouseup和click事件处理程序。如果我更改mouseup事件处理程序中的文本,则不会再调用click事件处理程序。为什么会发生这种情况 我在Chrome、Firefox和Opera中测试了以下代码: <!doctype html> <html> <head> <script type='text/javascript' src='jquery-1.7.1.js'></script>

我有一个Raphael文本元素,它具有mouseup和click事件处理程序。如果我更改mouseup事件处理程序中的文本,则不会再调用click事件处理程序。为什么会发生这种情况

我在Chrome、Firefox和Opera中测试了以下代码:

<!doctype html>
<html>
<head>
  <script type='text/javascript' src='jquery-1.7.1.js'></script>
  <script type='text/javascript' src='raphael-min.js'></script>
  <script type='text/javascript'>
window.onload = function() {
  var paper = Raphael(document.getElementById('raphael'),500,200);
  var text1 = paper.text(100, 20, "Just movin'");
  var text2 = paper.text(100, 40, "Hello World");

  $(text1.node).mouseup(function() {
    text1.attr({"x" : "200"});
    console.log("text1 mouseup");
  });
  $(text1.node).click(function() { 
    console.log("text1 click"); // Is called
  });

  $(text2.node).mouseup(function() {
    text2.attr({"text": "Goodbye world"});
    console.log("text2 mouseup");
  });
  $(text2.node).click(function() { 
    console.log("text2 click"); // Is not called
  });
}
  </script>
</head>
<body>
<div id="raphael"></div>
</body>
</html>

window.onload=函数(){
var paper=Raphael(document.getElementById('Raphael'),500200);
var text1=paper.text(100,20,“刚刚移动”);
var text2=paper.text(100,40,“Hello World”);
$(text1.node).mouseup(函数(){
text1.attr({“x”:“200”});
console.log(“text1 mouseup”);
});
$(text1.node)。单击(函数(){
console.log(“text1 click”);//被调用
});
$(text2.node).mouseup(函数(){
text2.attr({“text”:“再见世界”});
console.log(“text2mouseup”);
});
$(text2.node)。单击(函数(){
console.log(“text2 click”);//未调用
});
}

通过这样做,您将删除dom
text2.node
,并替换为一个新的dom,该dom附加了“仅单击事件”

所以试着这样做

 $(text2.node).mouseup(function(event) {
    // event.target is tspan
    // event.target.childNodes[0] is TextNode
    event.target.childNodes[0].nodeValue = "BYE WORLD";
    console.log("text2 mouseup", this);
  });

我对它进行了测试,效果很好。

可能是因为您使用jQuery并与Dom而不是SVG Dom交谈,我稍后会在iPod上进行测试。这解决了Chrome中的问题,但Firefox不再支持
replaceWholeText
,看到其他想法了吗?哦。。然后是event.target.childNodes[0].nodeValue=“BYE WORLD”;应该很好。谢谢,这很有效。为了反映这一点,我改变了你的答案。