簡體   English   中英

.on()沒有在ajax加載的頁面上工作

[英].on() not working on ajax loaded page

我的soft.php代碼

<!DOCTYPE html>
<html>
<head><style>
</style>
<script src="jquery.js"></script>
<script>
$(function(){
$("button").on("click",function(){alert("f");});
$("div").on("click",function(){
$("span").load("check.php");
});

});</script></head><body>
<div>Click</div>
<button>eaf</button><span></span>
</body></html>

和check.php代碼

<body>
<button>Hello</button>
</body>

我想通過點擊按鈕來提醒f,該按鈕是通過ajax加載的,它與.live()工作正常。 但是有人告訴我.on()可以用來代替live。但是.on()在這里不起作用。解決方案

$('body').on("click", "button", function(){
    alert("f");
});

當您在頁面加載后將button添加到DOM時,您需要使用委托事件。

最好使用其他選擇器代替body

你可以使用多個選擇器,如下面的:

$('body').on("click", "button, div", function(){
    alert("f");
});

閱讀.on()

暫無
暫無

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

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