2010-12-15 24 views
8

Estoy usando Raphael para dibujar algunos elementos en un sitio web. Los elementos incluyen rectángulo, línea (camino). He dado un id al elemento path e intento acceder a él en el evento onclick de esa línea. pero cuando hago una alerta de la identificación, nada es visible. Siguiente es el fragmento de códigoCómo acceder al atributo id de cualquier elemento en Raphael

function createLine() 
{ 
    var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight); 
    t.attr('stroke-width','3'); 
    t.attr('id','Hello'); 
    t.node.onclick = processPathOnClick; 
} 

function processPathOnClick() 
{ 
    alert($(this).attr("id")); 
} 

¿Puede alguien decirme cuál es el problema con el código anterior. Cualquier puntero será útil.

Gracias

Respuesta

14

¿Estás seguro de que no desea escribir $(t.node).attr('id','Hello'); en su lugar?

Actualización: alguien bajó la fecha de esta respuesta. Y realmente me siento obligado a señalar que esta forma de establecer el ID no es particularmente buena. Usted sería mejor usar:

t.node.id = 'Hello'; 

Me gustaría que hubiera una manera de acreditar Juan Mendes, que no sea Upvoting su comentario a esta respuesta.

+0

intenté eso, pero no funcionó – sgbharadwaj

+15

Esto debería funcionar, pero estoy desconcertado sobre por qué las personas usan jquery para establecer la identificación de un nodo, mucho ruido. Compare eso con 't.node.id = 'Hello'' –

+0

@sgbharadwaj Huh, lo intenté y funcionó para mí. ¿Reescribió a '$ (this.node) .attr ('id')' en el controlador? De todos modos, como se dijo, puedes escribir 't.node.it =" Hello "' y 'alert (this.id)' en el controlador- – Zecc

2

intente configurar el controlador usando jQuery

function createLine() 
{ 
    var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight); 
    t.attr('stroke-width','3'); 
    t.attr('id','Hello'); 
    $(t.node).click(processPathOnClick); 
} 

function processPathOnClick() 
{ 
    alert($(this).attr("id")); 
} 
+1

Hola Juan, Configurar el controlador no funcionó. Cambié el atributo de configuración a t.node.setAttribute ('id', pathId); y accediendo a alertar ($ (this) .attr ('id')); esto funcionó – sgbharadwaj

+0

Bueno, entonces eso te dice que establecer el id en el objeto de Raphael no lo establece en el nodo. No es necesario usar jquery para establecer la identificación. Su código sería mucho más simple haciendo 't.node.id = 'my-id'', y su manejador podría simplemente usar' alert (this.id) ' –

+0

Gracias que ayudó – sgbharadwaj

3

Prueba esto:

function createLine() { 
    var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight); 
    t.attr('stroke-width','3'); 
    t.id = 'Hello'; 
    t.node.onclick = processPathOnClick; 
} 

function processPathOnClick() { 
    alert($(this).id); 
    alert(this.id); // This should work too... 
} 

Básicamente va a crear una nueva propiedad llamada "id" en su línea Raphael variable de instancia "t". Es una especie de piratería, en mi opinión, pero funciona bien.

Cuestiones relacionadas