Som*_*nha 3 javascript jquery function this parameter-passing
我的代码是:
$(document).ready(function(){
var hCount = 0,
eCount = 0,
nCount = 0,
mCount = 0;
$("#head").click(function() {
var pPos = counter(hCount);
$(this).animate({left:pPos+"px"}, 1000);
});
function counter(count)
{
count++;
if(count === 10)
{
count = 0;
pPos = 0;
}
else
{
var pPos = $(this).css('left');
pPos = pPos.substring(0,(pPos.length-2))
pPos -= 367;
}
return pPos;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误说明
未捕获的TypeError:无法读取未定义的属性"defaultView"
我不知道是什么导致了这个错误.
另外,我怎么能传递给函数counter()的值$(this)的$("#head").click?我不能直接提及$("#head")因为我将重复使用更多div的功能#head,而不是在重用函数计数器中的代码时.
只需使用elem参数扩展计数器函数并在单击处理中传递它:
function counter(count, elem){
// ...
}
$("#head").click(function() {
var elem = $(this);
var pPos = counter(hCount, elem);
elem.animate({left:pPos+"px"}, 1000);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
135 次 |
| 最近记录: |