jQuery:在加载了ajax的元素上更改CSS? [英] jQuery: Changing the CSS on an element loaded with ajax?
问题描述
我需要更改使用ajax
加载的元素的位置.我想使用.css()
进行更改,但jQuery找不到导致其无法识别的元素.如何使jQuery识别"元素?
I need to change the position of an element i'm loading with ajax
. I want to use .css()
to change it but jQuery can't find the element cause it's not being recognized. How do i do to make jQuery "recognize" the element?
我已经读过关于live()
和delegate()
的信息,但是我不能让它们中的任何一个都能正常工作.非常感谢您的帮助!
I've read about live()
and delegate()
but i can't get neither one of them to work as i want them to. I'd really appreciate some help!
推荐答案
在ajax调用的complete
或success
函数中进行css更改.假设您正在使用load
:
Make the css change in the complete
or success
function of the ajax call. Assuming you're using load
:
$('#el').load(
url,
data,
function(){
$('#selector').css('position', 'absolute');
}
);
或者(更容易举个例子)注册一个ajaxComplete
事件
Alternatively (and easier to give as an example) register an ajaxComplete
event
$(document).ajaxComplete(function(){
if($('#elementID').length != 0) {
$('#elementID').css('position', 'absolute');
}
});
每次ajax请求完成时都会触发此操作,请检查#elementID是否存在以及是否存在css.
This is fired each time an ajax request completes, checks to see if #elementID exists and if so applies the css.
这篇关于jQuery:在加载了ajax的元素上更改CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!