So i'm trying to make a section fade out before following a link like so
所以我试图在跟随这样的链接之前使一个部分淡出
<a class="fadelink" href="path/to/file">Hello</a>
<script type="text/javascript">
$("a.fadelink").click(function(e){
e.preventDefault();
$("#content").fadeTo(400,0,function(){
window.location.href = $(this).attr("href");
});
});
</script>
Problem is, jQuery keeps returning me with "undefined" and 404's the redirect.
问题是,jQuery一直以“未定义”返回我,而404则是重定向。
2 个解决方案
#1
9
Your $(this)
in $(this).attr("href")
is pointing to $("#content")
which is wrong. Move it into the right scope:
你的$(this)in $(this).attr(“href”)指向$(“#content”),这是错误的。将其移至正确的范围:
$("a.fadelink").on("click", function( evt ) {
evt.preventDefault();
var goTo = $(this).attr("href"); // get href value of `this` anchor
$("#content").fadeTo(400, 0, function() {
window.location = goTo; // Now you can use it
});
});
#2
2
You're referring to the wrong this
. The href
attribute is present on the anchor, not the #content
element.
你指的是错误的。 href属性存在于锚点上,而不是#content元素。
$("a.fadelink").click(function(e){
e.preventDefault();
var that = this;
$("#content").fadeTo(400,0,function(){
window.location.href = $(that).attr("href");
});
});
#1
9
Your $(this)
in $(this).attr("href")
is pointing to $("#content")
which is wrong. Move it into the right scope:
你的$(this)in $(this).attr(“href”)指向$(“#content”),这是错误的。将其移至正确的范围:
$("a.fadelink").on("click", function( evt ) {
evt.preventDefault();
var goTo = $(this).attr("href"); // get href value of `this` anchor
$("#content").fadeTo(400, 0, function() {
window.location = goTo; // Now you can use it
});
});
#2
2
You're referring to the wrong this
. The href
attribute is present on the anchor, not the #content
element.
你指的是错误的。 href属性存在于锚点上,而不是#content元素。
$("a.fadelink").click(function(e){
e.preventDefault();
var that = this;
$("#content").fadeTo(400,0,function(){
window.location.href = $(that).attr("href");
});
});