Javascript jQuery$(this.find)
我想在单击链接时在名为“slide”的div上制作一个简单的动画;然后转到新文档(a中的href)。 到目前为止,我想出了这个Javascript jQuery$(this.find),javascript,jquery,Javascript,Jquery,我想在单击链接时在名为“slide”的div上制作一个简单的动画;然后转到新文档(a中的href)。 到目前为止,我想出了这个 $('a').click(function (event) { event.preventDefault(); $(this).find('.slide').animate({ right: '-100%' //animation on absolute positioned div },1000, function() {
$('a').click(function (event) {
event.preventDefault();
$(this).find('.slide').animate({
right: '-100%' //animation on absolute positioned div
},1000, function() {
location.href = $(this).attr("href");
});
});
什么也没发生。
如果我摆脱了“这个”-就像这样
$('a').click(function (event) {
event.preventDefault();
$('.slide').animate({
right: '-100%' //animation on absolute positioned div
},1000, function() {
location.href = $(‘a’).attr("href");
});
});
…动画效果很好,但不是跟随选定的链接,而是跟随页面上的第一个链接(可以理解)
有人知道第一个示例有什么问题吗?您需要将$(此)
放在不同的变量中
$('a').click(function (event) {
event.preventDefault();
var $this = $(this); // <------------------- like this
$(this).find('.slide').animate({
right: '-100%'//animation on absolute positioned div
},1000, function() {
location.href = $this.attr("href"); // <--------- use $this here
});
});
$('a')。单击(函数(事件){
event.preventDefault();
var$this=$(this);//只需使用event.target
:
$('a').click(function (event) {
event.preventDefault();
$('.slide').animate({
right: '-100%' //animation on absolute positioned div
},1000, function() {
location.href = $(event.target).attr("href");
});
return false;
});
尝试文档就绪并处于活动状态
$( document ).ready(function() {
$('a').on('click','',function (event) {
event.preventDefault();
$(this).find('.slide').animate({
right: '-100%' //animation on absolute positioned div
},1000, function() {
location.href = $(this).attr("href");
});
});
});
你为什么不把它叫做$a
或是$this
以外的东西,以免与$(this)
@RokoC.Buljan混淆,这一行清楚地表明了-->
location.href=$('a').attr(“href”);
@MohammadAdil oh right.但不是这一行$(this).查找('.slide.).slide>。动画({/code>)为什么要使用$?你可以很容易地做到:var that=$(这个);不要忘记var,否则,你是在全局声明它。@dekomote$
只是为了明确它是一个jquery变量。。是的var
应该在那里。$(这个)。查找('.slide').animate()和('.slide').animate())在不同的上下文中可能会有很大的差异。第一个要求幻灯片是当前元素的后代。第二个幻灯片从1.7版开始不推荐使用,从1.9版开始删除。