Javascript 在(文档)中设置变量。准备好所有函数了吗?
对jQuery来说真的很陌生,这让我困惑了一段时间 我将以此为例:Javascript 在(文档)中设置变量。准备好所有函数了吗?,javascript,jquery,Javascript,Jquery,对jQuery来说真的很陌生,这让我困惑了一段时间 我将以此为例: $(document).ready(function(){ $("#link1 a").hover(function(){ var $this = $(this); $this.css("color", "#fff"); }); }); $(document).ready(function(){ var $this
$(document).ready(function(){
$("#link1 a").hover(function(){
var $this = $(this);
$this.css("color", "#fff");
});
});
$(document).ready(function(){
var $this = $(this);
$("#link1 a").hover(function(){
$this.css("color", "#fff");
});
$("#link2 a").hover(function(){
$this.css("color", "#f00");
});
});
很明显,这会将悬停时的内部链接1的颜色更改为白色
这就是我想做的:
$(document).ready(function(){
$("#link1 a").hover(function(){
var $this = $(this);
$this.css("color", "#fff");
});
});
$(document).ready(function(){
var $this = $(this);
$("#link1 a").hover(function(){
$this.css("color", "#fff");
});
$("#link2 a").hover(function(){
$this.css("color", "#f00");
});
});
这不管用
我是否能够设置var$this=$(this);内部(文档)。准备好了吗,它可以与内部的所有功能一起工作
抱歉,如果这是一个愚蠢的问题,在其他地方找不到答案。如果我诚实的话,我不是100%确定要搜索 没有,但您可以选择所有内容-
$(document).ready(function() {
$this = $('*'); // all elements in the DOM for this page
});
不过我不推荐,你为什么要这样做?不,但你可以选择所有选项-
$(document).ready(function() {
$this = $('*'); // all elements in the DOM for this page
});
但是我不推荐它,为什么要这样做?您的语句
var$this=$(this)代码>虽然有效,但无法实现您需要的功能。如果你仔细想想。。。这是指$(文档)
因此,如果您将代码更改为:
$(document).ready(function(){
$("#link1 a").hover(function(){
var $this = $(this);
$this.css("color", "#fff");
}); //In this case $this refers to $("#link1 a")
$("#link2 a").hover(function(){
var $this = $(this);
$this.css("color", "#f00");
}); //In this case $this refers to $("#link2 a")
});
但是,这并不是真正必要的,因为您可以这样做:
$(document).ready(function(){
$("#link1 a").hover(function(){
$(this).css("color", "#fff");
});
$("#link2 a").hover(function(){
$(this).css("color", "#f00");
});
});
现在,如果您想增加$this
的范围,可以这样做:
$(a).hover(function() {
var $this = $(this);
//now if you wanted to check for which link is currently hovered you can say this :
var link = $this.attr("id");
//this will set link equal to the current id
//NOW you can have if statements checking which link it is...
if(link == "link1") { ... do stuff }
if(link == "link2") { ... do other stuff }
}
$(a).on("hover", function () {
...some function
});
此外,如果您使用的是JQuery版本1.7之后的版本,则应该调用如下事件:
$(a).hover(function() {
var $this = $(this);
//now if you wanted to check for which link is currently hovered you can say this :
var link = $this.attr("id");
//this will set link equal to the current id
//NOW you can have if statements checking which link it is...
if(link == "link1") { ... do stuff }
if(link == "link2") { ... do other stuff }
}
$(a).on("hover", function () {
...some function
});
最后,不要害怕查看jQueryAPI来获得一些帮助。。。它写得非常好,并提供了示例
您的语句var$this=$(this)代码>虽然有效,但无法实现您需要的功能。如果你仔细想想。。。这是指$(文档)
因此,如果您将代码更改为:
$(document).ready(function(){
$("#link1 a").hover(function(){
var $this = $(this);
$this.css("color", "#fff");
}); //In this case $this refers to $("#link1 a")
$("#link2 a").hover(function(){
var $this = $(this);
$this.css("color", "#f00");
}); //In this case $this refers to $("#link2 a")
});
但是,这并不是真正必要的,因为您可以这样做:
$(document).ready(function(){
$("#link1 a").hover(function(){
$(this).css("color", "#fff");
});
$("#link2 a").hover(function(){
$(this).css("color", "#f00");
});
});
现在,如果您想增加$this
的范围,可以这样做:
$(a).hover(function() {
var $this = $(this);
//now if you wanted to check for which link is currently hovered you can say this :
var link = $this.attr("id");
//this will set link equal to the current id
//NOW you can have if statements checking which link it is...
if(link == "link1") { ... do stuff }
if(link == "link2") { ... do other stuff }
}
$(a).on("hover", function () {
...some function
});
此外,如果您使用的是JQuery版本1.7之后的版本,则应该调用如下事件:
$(a).hover(function() {
var $this = $(this);
//now if you wanted to check for which link is currently hovered you can say this :
var link = $this.attr("id");
//this will set link equal to the current id
//NOW you can have if statements checking which link it is...
if(link == "link1") { ... do stuff }
if(link == "link2") { ... do other stuff }
}
$(a).on("hover", function () {
...some function
});
最后,不要害怕查看jQueryAPI来获得一些帮助。。。它写得非常好,并提供了示例
这都是关于范围的。不要在悬停事件之外定义此
。您希望它位于事件处理程序中,因此它引用被悬停的元素。这是一个愚蠢的问题,因为该范围中的this
是文档,不能使用样式属性设置样式。正如前面提到的,这没有真正的意义,因为在每个事件处理程序中,this
是不同的。$this
的值不会因访问位置的不同而发生神奇的变化。它只与作用域有关。不要在悬停事件之外定义此
。您希望它位于事件处理程序中,因此它引用被悬停的元素。这是一个愚蠢的问题,因为该范围中的this
是文档,不能使用样式属性设置样式。正如前面提到的,这没有真正的意义,因为在每个事件处理程序中,this
是不同的。$this
的值不会随访问位置的不同而发生神奇的变化。所以我想我问这个问题的原因是因为我不想为一件事定义$this。我想在更广阔的范围内定义它,这样我就可以多次使用它了?“我想这是个坏习惯吧?”尼克:好吧,在外面给它下定义的目的是什么?您不需要它来定位被悬停的元素,而不是影响同一元素的所有链接吗?$(这)
可以设置为一组元素,您只需正确操作即可。如果您想对每个链接项使用它,可以说类似于$(a).each(function(){var$this=$(this)…})
然后在任何情况下,$this
都将引用当前的
元素谢谢您的帮助:)所以我想我问这个问题的原因是因为我不想为一件事定义$this。我想在更广阔的范围内定义它,这样我就可以多次使用它了?“我想这是个坏习惯吧?”尼克:好吧,在外面给它下定义的目的是什么?您不需要它来定位被悬停的元素,而不是影响同一元素的所有链接吗?$(这)
可以设置为一组元素,您只需正确操作即可。如果您想对每个链接项使用它,可以说类似于$(a).each(function(){var$this=$(this)…})
然后在任何情况下,$此
将引用当前的
元素感谢您的帮助:)对我来说都是有意义的。这根本不能回答问题,也不能解释为什么OP的代码不能像他期望的那样工作。他的问题集变量在(文档)中。准备好所有函数了吗?通过选择all来回答,即使我不推荐。您是回答标题还是回答问题?他的标题是一个问题:)此外,在他的帖子中,他问:“我能设置var$this=$(this);inside(document)。准备好了吗?它可以与它内部的所有函数一起工作?”我回答“不,但是…”对我来说很有意义。这根本不能回答问题,也不能解释为什么OP的代码不能像他期望的那样工作。他的问题集变量在(文档)中。准备好所有函数了吗?通过选择all来回答,即使我不推荐。您是在回答标题还是在回答问题?他的标题是一个问题:)此外,在他的帖子中,他问:“我能设置var$this=$(this);inside(document)。准备好了吗?它可以与其中的所有函数一起工作?”我回答“不,但是…”