当页面加载和点击链接时触发jQuery函数?
问题描述:
我使用这个功能:当页面加载和点击链接时触发jQuery函数?
$(document).ready(function() {
$('.gdthumbtext')
.contents()
.filter(function(){
return this.nodeType == 3;
}).each(function(){
this.nodeValue= this.nodeValue.replace(/\+/g,'');
});
});
到.gdthumbtext
格内数字之前删除加号。 现在的问题是,这一数目将与阿贾克斯每次改变你点击一个链接 带班.gdt-starrating.
我想我有链接被点击时要调用(或调用)再次证明功能。
也许我需要这样的东西:当页面加载时调用函数或当点击链接类.gdt-starrating
时。
如何做到这一点的任何建议?
答
您可以将代码移到它自己的函数中,调用该函数onload,然后在每次单击链接时继续调用该函数。像这样:
$(document).ready(function() {
// Call cleanup onload
stripPlusSigns();
$('a.gdt-starrating').click(function() {
// Do stuff here...
// ...
// Strip plus signs again
stripPlusSigns();
});
});
function stripPlusSigns()
{
$('.gdthumbtext')
.contents()
.filter(function(){
return this.nodeType == 3;
}).each(function(){
this.nodeValue= this.nodeValue.replace(/\+/g,'');
});
return true;
}
答
您有正确的想法。
你应该在加载ajax内容后在ajax Sucess上运行它。我已经包含了click和ajax。
$(document).ready(function() {
// run on page load
removeSigns();
// you could run on ajax load
$('.gdt-starrating').bind('click', function() {
removeSigns();
});
// you should be running it on Ajax success callback
$.ajax({
// your normal ajax stuff...
success : function() {
removeSigns();
}
});
});
function removeSigns() {
$('.gdthumbtext')
.contents()
.filter(function(){
return this.nodeType == 3;
}).each(function(){
this.nodeValue= this.nodeValue.replace(/\+/g,'');
}
); }
奇怪的是,没有任何建议奏效。 – alexchenco
你能提供一个到你的代码的链接吗? –