如何执行Jquery代码只有一次? [英] How to execute Jquery code only once??
本文介绍了如何执行Jquery代码只有一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个jquery方法,我使用调用该方法点击一个按钮。
在那个代码中,我有一行 $(#loadingMessage)。css('padding-top','6%');
i需要这行执行只有一次,当我们第一次调用该方法,后来我走这条线。
I have one jquery method and i used call that method on click of a button .
So in that code i have one line " $("#loadingMessage").css('padding-top','6%');
" i need this line execute only once when we call that method first time,later that i gone this line .
所以请帮我找到一种方法
So please help me to find one way
整个方法脚本在下面
$('#SearchButton').click(function() {
$(".spinner").css('visibility','hidden');
$("#loadingMessage").css('padding-top','6%'); //I want this line execute only once, i mean first time
loaderStart();
document.getElementById('loadinggif3').style.display = "block";
$("#loadinggif3").show();
$(".col-sm-9").css('visibility','hidden');
var str = $('#SearchText').val();
str = str.trim();
if(str=="") return false;
)};
推荐答案
使用 jQuery .one()。使用.one()方法时,事件处理程序函数只对每个元素运行一次。
Use jQuery .one(). When using .one() method, the event handler function is only run once for each element.
$('#SearchButton').one("click", function () {
$("#loadingMessage").css('padding-top', '6%');
});
$('#SearchButton').click(function () {
$(".spinner").css('visibility', 'hidden');
loaderStart();
document.getElementById('loadinggif3').style.display = "block";
$("#loadinggif3").show();
$(".col-sm-9").css('visibility', 'hidden');
var str = $('#SearchText').val();
str = str.trim();
if (str == "") return false;
});
这篇关于如何执行Jquery代码只有一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文