我使用 jQuery 为页面上的元素设置悬停回调。我现在正在编写一个模块,它需要为某些元素临时设置新的悬停行为。新模块无法访问悬停功能的原始代码。
我想在设置新的悬停功能之前存储旧的悬停功能,以便在完成临时悬停行为后可以恢复它们。
我认为这些可以使用jQuery.data()
函数存储:
//save old hover behavior (somehow)
$('#foo').data('oldhoverin',???)
$('#foo').data('oldhoverout',???);
//set new hover behavior
$('#foo').hover(newhoverin,newhoverout);
Run Code Online (Sandbox Code Playgroud)
做一些新的悬停行为......
//restore old hover behaviour
$('#foo').hover($('#foo').data('oldhoverin'),$('#foo').data('oldhoverout'));
Run Code Online (Sandbox Code Playgroud)
但是如何从 jQuery 获取当前注册的悬停功能?
Shadow2531,我试图在不修改最初注册回调的代码的情况下做到这一点。否则你的建议会很好。感谢您的建议,并帮助澄清我正在寻找的内容。也许我必须进入jquery的来源并弄清楚这些回调是如何在内部存储的。也许我应该将问题更改为“是否可以在不修改 jquery 的情况下执行此操作?”
我正在构建一个基于ajax的导航,一切正常,除了一件事:
当我查看Firebug时,我可以看到每次点击时ajax网站都会被调用并加载两次.
这是脚本:
$(document).ready(function () {
//Check if url hash value exists (for bookmark)
$.history.init(pageload);
//highlight the selected link
$('a[href=' + document.location.hash + ']').addClass('selected');
//Seearch for link with REL set to ajax
$('a[rel=ajax]').click(function () {
//grab the full url
var hash = this.href;
//remove the # value
hash = hash.replace(/^.*#/, '');
//for back button
$.history.load(hash);
//clear the selected class and add the class class to the selected link
$('a[rel=ajax]').removeClass('selected');
$(this).addClass('selected');
//hide the main and show the progress bar
$('#main').hide();
$('#loading').show(); …
Run Code Online (Sandbox Code Playgroud) 我有div有一个onlcick事件.现在,当我单击div时,我想用jquery的.click属性更改其onlick函数.我可以动态地更改它,但是当我应用它时,新事件也会被触发.是否有任何解决方法,以便新的不被解雇但只是应用于具有新功能的div?这是我想要做的
我有一个div
<script type="text/javascript">
function testMe(data)
{
alert(data);
$('#testMe').unbind('click');
$('#testMe').click(function(){ testMe('2'); });
}
</script>
<div id='testMe' onclick='testMe(1)' >click on me, I will disappear.</div>
Run Code Online (Sandbox Code Playgroud)
当我执行上面的代码我仍然得到价值1 om警告框谢谢EveryOne
我也尝试了下面的代码,但它不适合我
function testMe(data)
{
alert(data);
$('#testMe').unbind('click');
$('#testMe').click( testMe(2) );
}
Run Code Online (Sandbox Code Playgroud)
虽然代码正在将onlick事件从'testMe(1)'更新为'testMe(2)',但它会不断提醒值2.围绕此问题的工作是传统的JS脚本代码:
document.getElementById('testMe').onclick = testMe(2) ;
Run Code Online (Sandbox Code Playgroud)
它的工作没有警报.
我有这个:
function test()
{
this.method = function ()
{
$("html").mousemove(function(event) {
console.log('~> moved');
});
}
this.method();
}
testInstance = new test();
testInstance = null; // delete window.testInstace;
Run Code Online (Sandbox Code Playgroud)
虽然我已经通过设置testInstance
为null 删除了对该对象的引用(我也尝试将其作为属性删除window
),但mousemove事件处理程序继续操作并写入控制台.如果删除建立事件处理程序的对象没有删除它,那么我该怎么做才能删除事件处理程序?
我想禁用点击功能,直到其中的每个代码; 初始化和完成.
我阅读了这些文章并尝试了他们所说的但是无法实现: event.preventDefault()与return false + 在jQuery中删除事件处理程序的最佳方法? + jQuery - 禁用单击直到所有链接的动画完成
我准备了这个问题的简单例子:
HTML
<div class="ele">1</div>
<div class="ele">2</div>
<div class="ele">3</div>
Run Code Online (Sandbox Code Playgroud)
jQuery的
$('.ele').click(function() {
if ( !$('.ele').is(':animated') ) {//this works too but;
//is:animate duration returns = 2000 but need to be 4000
$('.ele').stop().animate({'top':'0px'},2000);
$(this).stop().animate({'top':'100px'},4000);
}
return false;
});
?
Run Code Online (Sandbox Code Playgroud) 正如标题所说我试图禁用OnClick特定的
<div id="test" style="display: block; opacity: 0.89;"></div>
Run Code Online (Sandbox Code Playgroud)
这个div是从外部脚本加载的,它被混淆,所以我看不到具体的代码.
我试图删除这个
$("#test").click(function(){ return false});
$("#test").unbind();
$("#test").removeAttr("onclick");
//Suggestions which do not work
$('#test').on('click', function(e) {e.preventDefault();return false;});
$("#test").click(function(event){
event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
以上都不是.
编辑:使用.unbind("点击")解决了我的问题; div创建之后.
var town_imgs = $('.town_img img');
var container;
var imggg
town_imgs.hover(function(){
container = $('<div class="town_img_thmb">'
+'<span class="thmb_container ajax2"></span>'
+'</div>').appendTo($(this).parents('.town_wrapper').find('.thmb_wrapper'));
var imggg = new Image(140,100);
imggg.onload = function(){
container.find('.thmb_container').append(this);
}
imggg.src = $(this).attr('src').replace(/\/t0\//,'/t1/');
},function(){
container.remove();
$(imggg).unbind('onload');
});
Run Code Online (Sandbox Code Playgroud)
当我快速徘徊缩略图时不工作.它连续显示2-3张图像,大约250-500ms~
据我所知,这是因为我使用外部变量存储当前缩略图.
是吗?
是否有正确取消onload事件的解决方案?
谢谢 ;)
我做了一个井字游戏,我现在想知道如何在点击后使按钮无法点击.这是游戏领域:
<div id="gamefield">
<table border="0">
<tr>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
</tr>
<tr>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
</tr>
<tr>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
<td><img alt="" title="" src="img/empty.jpg" /></td>
</tr>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
这是randomstart函数:
var randomStart = Math.floor(Math.random() * 2);
Run Code Online (Sandbox Code Playgroud)
这是游戏领域的功能:
$("#gamefieldtr td").click(function() {
if ($(".game-button").html() == "Start spel") {
alert("you can't start");
} else …
Run Code Online (Sandbox Code Playgroud)