2008-10-03 17 views
7

Necesito agregar un tooltip/alt a un elemento "td" dentro de mis tablas con jquery.¿Cómo agregar una información sobre herramientas a un "td" con jquery?

¿Alguien me puede ayudar?

me trataron:

var tTip ="Hello world"; 
$(this).attr("onmouseover", tip(tTip)); 

donde he verificado que estoy usando el "TD" como "este".

** Editar: ** Puedo capturar el elemento "td" mediante el uso del comando "alertar" y funcionó. Entonces, por alguna razón, la función "punta" no funciona. Alguien sabe por qué esto sería?

Respuesta

24
$(this).mouseover(function() { 
    tip(tTip); 
}); 

una mejor manera podría ser poner title atributos en el código HTML. De esta forma, si alguien tiene el Javascript desactivado, recibirá una información sobre herramientas (aunque no tan bonita/flexible como se puede hacer con jQuery).

<table id="myTable"> 
    <tbody> 
     <tr> 
      <td title="Tip 1">Cell 1</td> 
      <td title="Tip 2">Cell 2</td> 
     </tr> 
    </tbody> 
</table> 

y luego usar este código:

$('#myTable td[title]') 
    .hover(function() { 
     showTooltip($(this)); 
    }, function() { 
     hideTooltip(); 
    }) 
; 

function showTooltip($el) { 
    // insert code here to position your tooltip element (which i'll call $tip) 
    $tip.html($el.attr('title')); 
} 
function hideTooltip() { 
    $tip.hide(); 
} 
+0

Quiero que esto funcione, pero por alguna razón no ... –

+0

¿Puede dar más detalles? ¿QUÉ SUCEDE? ¿Hay algún mensaje de error? ¿realmente tienes una función llamada "propina"? – nickf

1
var tTip ="Hello world"; 
$(this).mouseover(function() { tip(tTip); }); 
+0

Quiero que esto funcione, pero por alguna razón no ... –

-1

Si realmente desea poner esas información sobre herramientas en sus celdas de la tabla y no en los encabezados de la tabla, donde tendrían mucho más sentido, considere ponerlos en el contenido DENTRO de las celdas de la tabla, donde es mucho más significativo.

1

grdList - tabla ID

td: nth-child (5) - columna

$('#grdList tr td:nth-child(5)').each(function(i) { 
    if (i > 0) { //skip header 
     var sContent = $(this).text(); 
     $(this).attr("title", $(this).html()); 
     if (sContent.length > 20) { 
      $(this).text(sContent.substring(0,20) + '...'); 
     } 
    } 
}); 
3
$('#grdList tr td:nth-child(5)').each(function(i) { 
    if (i > 0) { //skip header 
     var sContent = $(this).text(); 
     $(this).attr("title", $(this).html()); 
     if (sContent.length > 20) { 
      $(this).text(sContent.substring(0,20) + '...'); 
     } 
    } 
}); 

grdList - tabla ID

td: nth-child (5) - columna 5

+0

Esto me ayudó mucho, sin embargo, tiene más sentido usar '$ (this) .text()' para el título en lugar de '$ (esto) .html() ', esp. si tienes hipervínculos en tus celdas – Alex

Cuestiones relacionadas