Domanda

Ho una tabella con dati come questo:

<table>
 <tr onclick="window.location='download.php?id=1'">
  <td>Program 1<br/>Short Description 1 (<a onclick="$.popup.show('Install Instructions', 'Instructions pulled from DB here')">Instructions</a>)</td>
  <td>Added by user and date/time here<td>
  <td>Filesize here<td>
  <td><a href="edit.php?id=1">Edit</a> - <a href="delete.php?id=1">Delete</a><td>
 </tr>
 (repeat TRs for more programs pulled from DB)
</table>

Il problema che sto riscontrando è che quando si fa clic sul collegamento (Istruzioni), dovrebbe apparire una finestra con le istruzioni. Lo fa, ma spara anche il clic del TR. Quindi ottengo il popup per una frazione di secondo, quindi va su download.php.

Esiste un modo per impedire che spari il clic del TR? Preferibilmente usando jquery, ma non mi dispiace neanche una semplice soluzione javascript.

È stato utile?

Soluzione

Fondamentalmente, è necessario interrompere la propagazione di eventi , utilizzando event.cancelBubble ( per IE) o event.stopPropagation () (per tutti gli altri). La fornita da quirksmode (che ho usato prima) è da fare qualcosa del genere:

<script type="text/javascript">
var instructions = function(e) {
    // Get the correct event in a cross-browser manner, then stop bubbling/propagation.
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();

    // Do what you want now
    $.popup.show('Install Instructions', 'Instructions pulled from DB here');
}
</script>

Quindi il tuo script lo chiamerebbe:

<td>Program 1<br/>Short Description 1 (<a onclick="instructions(event)">Instructions</a>)</td>

(Certo, potresti mettere tutto in linea, ma è un casino insanguinato.)

Puoi anche trovare questa domanda illuminante .

Altri suggerimenti

Puoi scrivere

onclick="false"

e aggiungi eventi usando il modello di eventi livello 2 (attachEvent in ie o addEventListener in altri browser). Puoi anche legare jquery ('click') se usi jquery.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top