Javascript Jquery函数没有';Ajax调用后无法工作
我有这个函数:Javascript Jquery函数没有';Ajax调用后无法工作,javascript,jquery,ajax,html,Javascript,Jquery,Ajax,Html,我有这个函数: $(document).ready(function() { $('.post_button, .btn_favorite').click(function() { //Fade in the Popup $('.login_modal_message').fadeIn(500); // Add the mask to body $('body').append('<div class="overlay"></div>'); $('.overlay'
$(document).ready(function() {
$('.post_button, .btn_favorite').click(function() {
//Fade in the Popup
$('.login_modal_message').fadeIn(500);
// Add the mask to body
$('body').append('<div class="overlay"></div>');
$('.overlay').fadeIn(300);
return false;
});
$(文档).ready(函数(){
$('.post_按钮,.btn_收藏夹')。单击(函数(){
//在弹出窗口中淡入淡出
$('.login_modal_message').fadeIn(500);
//将遮罩添加到主体
$('body')。追加('');
$('.overlay').fadeIn(300);
返回false;
});
“我的页面”使用常用按钮加载内容,但在Ajax调用并生成其他新内容后,当您单击新内容的按钮时,该功能不起作用。有什么不对的吗?这是因为您使用的是动态内容 您需要将单击调用更改为委托方法,如on
$('.post_button, .btn_favorite').on('click', function() {
或
我不确定我是否答对了你的问题,但你可能想试试
$.ajax({
url: "test.html"
}).done(function() {
$('.post_button, .btn_favorite').click(function() {
//Fade in the Popup
$('.login_modal_message').fadeIn(500);
// Add the mask to body
$('body').append('<div class="overlay"></div>');
$('.overlay').fadeIn(300);
return false;
});
$.ajax({
url:“test.html”
}).done(函数(){
$('.post_按钮,.btn_收藏夹')。单击(函数(){
//在弹出窗口中淡入淡出
$('.login_modal_message').fadeIn(500);
//将遮罩添加到主体
$('body')。追加('');
$('.overlay').fadeIn(300);
返回false;
});
只需将代码粘贴到done
函数中即可
希望有帮助:)
编辑:
我还注意到您的问题缺少};
。而不是这个:
$('.post_button, .btn_favorite').click(function() {
这样做:
$(document).on('click','.post_button, .btn_favorite', function() {
上的将使用与选择器匹配的当前元素和未来元素
干杯如果您知道.post_按钮、.btn_收藏夹的容器,请使用
$('#container_id').on('click', '.post_button, .btn_favorite', function () { });
因此,如果未找到”.post_按钮、.btn_favorite'
,则它将冒泡到容器id
否则,如果您不知道容器,则将其委托给文档
$(document).on('click', '.post_button, .btn_favorite', function () { });
以下几点对我很有用
$(document).ready(function(){
$(document).bind('contextmenu', function(e) {
if( e.button == 2 && jQuery(e.target).is('img')) {
alert('These photos are copyrighted by the owner. \nAll rights reserved. \nUnauthorized use prohibited.');
return false;
}
});
});
一旦ajax内容被替换为旧内容,就需要绑定jQuery单击事件
在AJAX成功块中,您需要添加如下代码新的响应html内容一个标签,如
<a href="#" id="new-tag">Click Me</a>
元素类
是应用的元素类。它是此处的选择器
$(document).on("click", ".class-of-element", function (){
alert("Success");
});
还有你的问题中的html和ajax调用。嗯,那么为什么我在FireBug中得到“TypeError:$(…).on不是函数?”呢?@SauliusSlavinskas可能是你定义了$(…)的选择器在这里面没有被选中,即没有返回objectWell,我发现了问题…似乎我使用的是早于1.72的Jquery库。感谢您的帮助!抱歉:),您的意思是在ajax完成后,单击
函数将不会执行吗?很好地解释了为什么在
上使用解决了问题。使用$(文档)
也为我解决了同样的问题。
$("#new-tag").click(function(){
alert("hi");
return false;
});
$(document).on("click", ".class-of-element", function (){
alert("Success");
});