好吧,如果我误解或曲解了任何东西,很抱歉,但我对jQuery和javascript还是相当陌生的。
我用php建立了一个商店,其中一个元素是jQuery购物车,它在所有页面中都是‘必需的’,所以它总是在页面上。我在页面上包含的使其工作的jQuery如下所示:
$(document).ready(function(){
$("#cbox").hover(function(){
$(this).stop(true,false)
.animate({right: 0}, 500); },
function(){
$("#cbox").stop(true,false).animate({right: -120}, 500); });
});基本上,购物车位于页面的右侧,悬停时弹出/滑出。
我想让它以编程方式弹出/滑出,当用户添加项目到篮子时,作为事件的一种通知。理想情况下使用onclick。
我试过调用不同的部分,但由于它是一个文档就绪的整体函数,我正在为我的onclick应该说什么而苦苦挣扎。我甚至不确定它是否会起作用。
有人能给我指出正确的方向吗?
发布于 2012-11-30 20:43:33
$(document).ready(function(){
$("#cbox").hover(function()
{
$(this).stop(true,false)
//.animate({right: 0}, 500); },function()<-- invalid syntax: leave out parentheses and semi-colon:
.animate({right: 0}, 500,function()
{//no need for the selecot here
$(this).stop(true,false).animate({right: -120}, 500);
});
});
$('.storeItems').on('click',function()
{//best use on for AJAX content, for example
$('#cbox').trigger('hover');/
});
});//you were missing this closing我有一个建议:.on('click'处理程序中有一个jQuery选择器。这意味着您将在每次单击事件时扫描该#cbox元素的DOM。您可以在此处使用闭包来提高代码的效率:
$('.storeItems').on('click',(function(cbox)
{
return function()
{//cbox is a reference to the cart element, in memory so no need to scan the dom over and over
cbox.trigger('hover');
};
}($('#cbox'))));//pass the reference here我知道,有很多function,甚至还有更多的括号和括号。你不必使用这种方法,但只要在你开始学习更多JS时记住这一点:我保证,你迟早会开始使用闭包:)
发布于 2012-11-30 20:22:05
单击另一项时调用hoverfunction是众多解决方案之一
$(document).ready(function(){
$("#cbox").hover(function(){
$(this).stop(true,false)
.animate({right: 0}, 500); },
function(){
$("#cbox").stop(true,false).animate({right: -120}, 500); });
});
$('.itemClicked').click(function(e){
$('#cbox').trigger('hover');
});});
https://stackoverflow.com/questions/13645114
复制相似问题