我有一張表列出一些評論。在評論旁邊有兩個鏈接,hide
和show
。所以我想要做的是當點擊右邊的鏈接來改變評論的狀態與Ajax。點擊鏈接隱藏/顯示內容
// inside a loop for showing all comments
<div class="pull-right" class="publish-history-comment">
<a href="#" data-time="<?= $row->time; ?>" class="publish-history-comment-link" onclick="toggleHistoryNotes('publish');">Publish</a>
</div>
<div class="pull-right" class="hide-history-comment">
<a href="#" data-time="<?= $row->time; ?>" class="hide-history-comment-link" onclick="toggleHistoryNotes('hide');">Hide</a>
</div>
<?= $row->comment; ?>
<script type="text/javascript">
function toggleHistoryNotes(status) {
var link = $('.' + status + '-history-comment-link');
var time = link.attr('data-time');
alert(time);
}
</script>
如何定位點擊了哪個鏈接並執行ajax調用以切換評論狀態?
嘗試也許'$(本).attr( '數據時間')' –
只是一些額外的信息:警告沒有火起來,但是當我點擊鏈接之一,而不是它滾動頁面頂部 – Lykos
嘗試添加e.preventDefault()到開始 –