jquery、親要素のonclick = window.locationを防ぐ
-
03-07-2019 - |
質問
次のようなデータを含むテーブルがあります:
<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>
私が抱えている問題は、(Instructions)リンクをクリックすると、そのウィンドウに指示が表示されるはずだということです。それは行いますが、TRのonclickも起動します。だから一瞬ポップアップが表示されたら、download.phpに行きます。
どういうわけか、TRのonclickを発生させないようにできますか?できればjqueryを使用しますが、プレーンなJavaScriptソリューションも気にしません。
解決
基本的に、event.cancelBubbleを使用して、イベントの伝播を停止する必要があります( IEの場合)またはevent.stopPropagation()(それ以外の場合)。 quirksmodeから提供されたソリューション(以前に使用したこと)は、このようなもの:
<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>
その後、スクリプトはそれを呼び出します:
<td>Program 1<br/>Short Description 1 (<a onclick="instructions(event)">Instructions</a>)</td>
(もちろん、これをすべてインラインにすることもできますが、それは血まみれの混乱です。)
この質問も照らす。
他のヒント
stopPropogationは必要なものです。
http://docs.jquery.com/Events/jQuery .Event#event.stopPropagation.28.29
書くことができます
onclick="false"
イベントモデルレベル2(ieのattachEventまたは他のブラウザーのaddEventListener)を使用してイベントを追加します。 jqueryを使用している場合は、jqueryバインド(「クリック」)することもできます。
所属していません StackOverflow