How do I return an elements class in jQuery?
I would like to send the class of an element to a function. Basically, I need to tell showAjaxLoader() that it should show the loading icon for the element being clicked!
jQuery(function($) {
var showAjaxLoader = function(selector) {
$(selector).empty().html('<img src="loading.gif" />');
};
开发者_C百科 $(".add")
.bind("ajax:beforeSend", function(event, data, status, xhr) {
var class = this.class;
showAjaxLoader(class);
});
});
class
is a reserved word in Javascript, so you can't use it as a variable name. It's not available as a property of an element: you need to use the className
property instead.
jQuery(function($) {
var showAjaxLoader = function(message) {
alert(message)
};
$(".add")
.bind("ajax:beforeSend", function(event, data, status, xhr) {
var className = this.className;
showAjaxLoader(className);
});
});
精彩评论