簡體   English   中英

e.preventDefault不適用於live()事件

[英]e.preventDefault not working on live() event

關於此腳本一切工作得很好,但除了e.PreventDefault(); 我已經嘗試了一切,但似乎沒有任何效果。 我不能在此腳本中使用.click()處理函數,因為它是動態調用的,否則我會嘗試使用它。

JS

<script>
$('#vote_up').live('click', function(e) {
    e.preventDefault();
    $.post("votes.php", {
        vote: "1",
        item_id: "$item_id",
        type: "$type"
    }, function(data) {
        $('#vote_count').empty().append(data);
    });
});
$('#vote_down').live('click', function(e) {
    e.preventDefault();
    $.post("votes.php", {
        vote: "-1",
        item_id: "$item_id",
        type: "$type"
    }, function(data) {
        $('#vote_count').empty().append(data);
    });
});
    </script>

的HTML

<div class="vote_box">
    <a href="#" id="vote_up">▲</a>
    <span id="vote_count">$count</span>
    <a href="#" id="vote_down">▼</a>
</div>

您可以只返回false wich,也可以防止觸發事件時發生其他事件。

$('#vote_up').live('click', function(e) {

$.post("votes.php", {
    vote: "1",
    item_id: "$item_id",
    type: "$type"
}, function(data) {
    $('#vote_count').empty().append(data);
});

return false;

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM