我正在尝试添加一个链接到另一个的fadeOut函数。CLICK HERE目前我有一个闪烁的徽标。当用户点击徽标时,闪烁停止,有一个轻微的延迟,然后慢慢淡出。有没有人能够纠正我在下面粘贴的代码?
<script>
$(document).ready(function(){
$("#center-gif").click(function(){
$('#center-gif').hide();
$('#center-img').show();
});
$('#center-img').click(function(){
$('#center-img').hide();
$('#center-img-gif').show();
});
$('flash-link').click(function(){
$('center-img').fadeOut(5000);
});
});
</script>发布于 2012-07-18 19:13:41
如果你想用class/id来访问元素,你必须在一开始就定义.和#,比如css。
下面是一些例子:
$('img').fadeOut();//selects all img elements
$('.img').fadeOut();//selects all elements with class="img"
$('myClass').fadeOut(); //false
$('.myClass').fadeOut(); //true
$('myId').fadeOut(); //false
$('#myId').fadeOut(); //true下面是用更少的代码解决你的问题的工作jQuery:
$(document).ready(function(){
$("img").click(function(){
var takeId = $(this).attr('id');//takes clicked element's id
$('img').hide();//hides all content
$('#'+takeId).show();
//matches clicked element's id with element and shows that
});
$('#flash-link').click(function(){//define '#' id declaration here
$('#center-img').fadeOut(5000,//new function after fadeOut complete
function() {
window.open('url','http://iamnatesmithen.com/jukebox/dancers.php');
return false;
});
);
});
});发布于 2012-07-18 19:18:12
所以我假设你的问题是图像不会淡出,对吧?
这可以解决这个问题:
首先将您的.click()-functions更改为:
$().click( function(event) {
// cour code
event.preventDefault();
}然后像这样改变最后一个:
$('#flash-link').click( function(event) {
$('#center-img').fadeOut( 5000, function() {
window.location.href = 'jukebox/dancers.php';
});
event.preventDefault();
});我没有对此进行测试,但它应该可以工作。它的作用是:淡出图像,并在准备好时调用函数。此函数随后会重定向到您的下一页。
event.preventDefault();将告诉浏览器不要委派click-event。如果你不把它放在那里,浏览器就会打开锚点而不等待任何JavaScript的执行。
备注
当您想要选择一个带有ID的元素时,使用这个选择器:$('#[id]')作为这个选择器,$('html')只适用于HTML-elements。
https://stackoverflow.com/questions/11539866
复制相似问题