Leo*_*een 6 javascript jquery function
我创建了一个小jquery脚本,我在自定义函数中使用(this)有问题.
这是代码:
jQuery("li").click(function()
{
var scrollTop = jQuery(window).scrollTop();
if(scrollTop > 0)
{
jQuery('html, body').animate( { scrollTop: 0 }, 'slow', function()
{
fadeItems();
});
}
else
{
fadeItems();
}
});
function fadeItems()
{
var slogan = jQuery(this).children('p').html();
jQuery('#slogan_text').fadeOut(150, function(){
jQuery('#slogan_text').fadeIn(150).html(slogan);
});
var content = jQuery(this).children('#post_content_large').html();
jQuery('#content_view').html(content).hide();
var status = jQuery("#readMore").html();
if(status == 'Verbergen')
{
jQuery('#content_view').fadeIn(500, function(){
jQuery('#content_view').fadeIn(500).html(content);
});
}
var title = jQuery(this).children('h3').html();
jQuery('#title_content').fadeOut(150, function(){
jQuery('#title_content').fadeIn(150).html(title);
});
}
Run Code Online (Sandbox Code Playgroud)
因此,当单击列表项时,该函数会运行,但是(this)的值为空
有人知道如何解决这个问题吗?
提前致谢!
.call在这里很有用:
jQuery("li").click(function () {
var self = this;
var scrollTop = jQuery(window).scrollTop();
if(scrollTop > 0) {
jQuery('html, body').animate( { scrollTop: 0 }, 'slow', function() {
fadeItems.call(self);
});
}
else {
fadeItems.call(self);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
471 次 |
| 最近记录: |