我想检查一个元素是否有类名.active,如果有,则添加: margin-top: 4px;
但是我只想让这个操作在页面加载时发生一次,一旦检测到该类名,就不想再次检测并添加CSS样式。
当特定元素被悬停时应用此类名。
在jQuery中是否可行?
我想检查一个元素是否有类名.active,如果有,则添加: margin-top: 4px;
但是我只想让这个操作在页面加载时发生一次,一旦检测到该类名,就不想再次检测并添加CSS样式。
当特定元素被悬停时应用此类名。
在jQuery中是否可行?
查看one事件。以下是文档中的示例:
$('#foo').one('click', function() {
alert('This will be displayed only once.');
});
$(function(){
if ($("#elementid").hasClass("active"))
{
$("#elementid").css("margin-top", "4px");
}
});
(function($) {
// my custom function to process elements
function myProcessFunction(context) {
// get context
context = context || document;
// select elements within the context, filter out already processed ones
// loop through remained (unprocessed) elements
// '.my-class' - selector for the elements I want to process
$('.my-class:not(.my-class-processed)', context).each( function(i.e){
// mark the element as processed
$(e).addClass('my-class-processed');
// add process code here
});
}
// run myProcessFunction on document.ready
$(document).ready( function(){
myProcessFunction();
});
})(jQuery);