and*_*ick 58 javascript jquery events
$('#myTable').click(function(e) {
var clicked = $(e.target);
clicked.css('background', 'red');
});
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释这个,并解释为什么需要e,以及它实际上做了什么..
Fra*_*kie 66
使用e只是一个简称event.您可以传递任何您想要的变量名称.
// would work just the same
$('#myTable').click(function(anyothername) {
var clicked = $(anyothername.target);
});
Run Code Online (Sandbox Code Playgroud)
您可以查看有关jQuery处理事件的更多信息.
Dav*_*ter 10
拥有e(引发事件的对象)的一个好处是允许您阻止某些元素的默认行为的传播.例如:
<a id="navLink" href="http://mysite/someOtherPage.htm">Click For Info</a>
Run Code Online (Sandbox Code Playgroud)
呈现用户可以单击的链接.如果用户禁用了JavaScript(为什么?我不知道),您希望用户在单击链接时导航到someOtherPage.htm.但是如果他们启用了JavaScript,那么您希望显示模式对话框而不是离开页面.您可以通过阻止锚/链接的默认行为并显示模式来处理此问题:
$("#navLink").click(function(e) {
e.preventDefault(); //this prevents the user from navigating to the someOtherPage.htm
$("#hiddenDiv").dialog({
//options
}); //show the dialog
});
Run Code Online (Sandbox Code Playgroud)
因此,使用该参数可以使您在其他答案中描述的其他内容中阻止所选元素的默认行为.
希望这可以帮助!
小智 8
我在理论上说我不是专家,但是通过使用他不需要的小(e)我达到了预期的效果
我想到了.这是将同一事件从一个函数传递到另一个函数的一种方法.
简单来说.我想让页面导航成为弹性滚动功能,但是,我希望页面通过悬停"和"进行导航,我希望在某些条件下可以点击相同的导航.我还想从其他非链接的点击事件中获得相同的动态导航.为了保持当前目标并仍然使用导航功能,我必须设置小(e),因为jQuery将失去$(this)的范围作为函数lol的相同目标.这是一个简单的例子.
function navigate_to_page(e){
var target = $(e.currentTarget).attr('href'); //--This is the same as $(this) but more static to bring out of it's scope
$('html, body').animate({
'scrollTop':$(target).offset().top-0,
'scrollLeft': $(target).offset().left-$(window).width()*0.0}, 2000, 'easeOutBounce');
}
Run Code Online (Sandbox Code Playgroud)
不要让胡言乱语混淆你.这只是一个简单的页面滚动动画.需要注意的是e.currentTarget.e是我们的变量,currentTarget是一个等价于$(this)的jQuery,所以这些是一个Globular $(this)函数.现在我用另一个具有类似条件的函数来调用它
$('#myNavigationDiv a').on('mouseenter', function(e){
if($(myCondition) === true){
return false;
}else{
navigate_to_page(e);
}
});
Run Code Online (Sandbox Code Playgroud)
看看小(e)如何将所有东西联系在一起?
现在你可以用(e)代替(wveryveryouwant).通过在两个函数中调用e,它匹配e .currentTarget,你可以将它应用于你需要的任何详细的特定函数,并保存自己的LITERALLY页面的代码lol
| 归档时间: |
|
| 查看次数: |
80277 次 |
| 最近记录: |