为什么 jQuery 不影响 Ajax 调用后生成的内容
我试图隐藏 div
中的所有 li
,该 div 是在用户单击按钮时动态生成的。 div
的内容通过 Ajax 调用提取,然后分配给页面上的 div。
但出于某种原因,任何使用 jQuery 改变样式的尝试都不会产生任何影响。我注意到在尝试触发点击时还必须应用 $(selector).live('click, callback)
。
$.ajax({
url: "admin.php",
cache: false,
data: ({
'module':'data',
'action':'actionName',
'clientname':formatted
}),
success: function(data) {
$('#outputDiv').html(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if(textStatus == "error") {
$('#outputDiv').append("Sorry, but there was an error");
}
}
});
The 'data' variable returned to the success method is a nested list set e.g. <ul id='tree'><il><a></a></li></ul> etc
What I'm trying to do after the load is hide all children in this case I'd call $(' #tree > li').hide(); A trigger will then occur when the use clicks the anchor tag with:
$('a.viewdetails').click(function() {
$(this).next('ul').toggle();
});
编辑 1
所以现在我已经实现了下面提到的解决方案,但我无法让负载正常运行,它似乎没有发送 url 参数,格式是在上面生成的,可以由空格等组成:
var containerDiv = $('div#clientBusinessUnits'),
liClickHandler = function(e) {
$('a.viewdetails').click(function() {
alert('1');
});
},
loadCompleteHandler = function(responseText, textStatus, XMLHttpRequest) {
console.log(responseText);
console.log(textStatus);
$('li', containerDiv).click(liClickHandler);
};
containerDiv.load("admin.php?module=data&action&getData&clientname="+formatted, loadCompleteHandler);
I'm attempting to hide all li
's within a div
which is dynamically generated when a use clicks a button. The content of the div
is pulled through an Ajax call and I then assign to a div on the page.
For some reason though, any attempt to alter the style with jQuery doesn't have any affect. I notice I also have to apply $(selector).live('click, callback)
when attempting to trigger a click.
$.ajax({
url: "admin.php",
cache: false,
data: ({
'module':'data',
'action':'actionName',
'clientname':formatted
}),
success: function(data) {
$('#outputDiv').html(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if(textStatus == "error") {
$('#outputDiv').append("Sorry, but there was an error");
}
}
});
The 'data' variable returned to the success method is a nested list set e.g. <ul id='tree'><il><a></a></li></ul> etc
What I'm trying to do after the load is hide all children in this case I'd call $(' #tree > li').hide(); A trigger will then occur when the use clicks the anchor tag with:
$('a.viewdetails').click(function() {
$(this).next('ul').toggle();
});
Edit 1
So now I've implemented the solution mentioned below but I can't get the load to function correctly, it doesn't appear to send the url parameters, formatted is generated above and can consist of spaces etc:
var containerDiv = $('div#clientBusinessUnits'),
liClickHandler = function(e) {
$('a.viewdetails').click(function() {
alert('1');
});
},
loadCompleteHandler = function(responseText, textStatus, XMLHttpRequest) {
console.log(responseText);
console.log(textStatus);
$('li', containerDiv).click(liClickHandler);
};
containerDiv.load("admin.php?module=data&action&getData&clientname="+formatted, loadCompleteHandler);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何绑定到特定元素的事件处理程序(在元素存在于页面上之前)都将不起作用。
.live() 方法对此有所帮助,因为它可以让您提前将事件处理程序绑定到动态插入元素后与选择器匹配的元素。
因此,在不知道您的实际代码的情况下,您的问题的解决方案是,要么
粗略地描绘了示例:
Any event handlers bound to specific elements - before the elements exist on the page - won't work.
The .live() method helps with that, in that it will let you bind event handlers in advance, to elements that match a selector after elements have been dynamically inserted.
So, without knowing your actual code, a solution for your problem would be to, either
Roughly sketched example:
这是因为代码仅在脚本加载后运行一次。如果您通过 ajax 调用更改 html,则需要重新运行代码以隐藏
li
。要实现此目的,您可以将代码放入一个函数中,并在站点加载后调用此函数,并且在站点更新后,如果您从 ajax 调用的回调函数中调用此函数,则可以执行此操作。It is because the Code is only run once after the script is loaded. If you change you html via an ajax call you need to rerun the code to hide the
li
s. To accomplish this you can put the code into a function and call this function once the site is loaded and once the site gets updated, you can do this if you call this function from within the callback function of the ajax call.