当涉及到 jQuery 语法时,我能摆脱什么?
我看到这个用了很多:
$("img").hide( 800, function() {
$(this).show();
});
...但是我们能得到同样的效果吗:
$("img").hide( 800, "$(this).show();" );
我看到这个用了很多:
$(this).hide();
...但是我们能得到同样的效果吗?像这样的影响:
this.hide();
...这个怎么样:
$("this").hide();
我看到这个用了很多:
element.click(function() {
notify();
});
...但是我们能得到像这样的相同的影响:
element.click(notify);
我的观点:
有吗我们通常使用的常见 jQuery 方法的简单版本?
I see this used a lot:
$("img").hide( 800, function() {
$(this).show();
});
...but can we get the same affect like this:
$("img").hide( 800, "$(this).show();" );
I see this used a lot:
$(this).hide();
...but can we get the same affect like this:
this.hide();
...how about this:
$("this").hide();
I see this used a lot:
element.click(function() {
notify();
});
...but can we get the same affect like this:
element.click(notify);
My point:
Are there simpler versions of the common jQuery methods we normally use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,不,你不能。如果
hide
的第二个参数是一个字符串,那么它应该是一个缓动并且"$(this).show();"
不是缓动;所以$("img").hide( 800, "$(this).show();" );
只会将难以理解的错误放入控制台,甚至不会隐藏任何内容。如果您看到以下内容:
使用了很多,那么您正在查看大量无用的代码,您应该开始查看更好的代码。
$(this).hide;
没有做任何有用的事情,也许它会运用 JavaScript 引擎的死代码删除算法,但它肯定不会隐藏页面上的任何内容。说$(this).hide;
并不比说42;
; 更有效率。当然,它们都是法律声明,但没有做任何有用的事情。也许您的意思是:
但这与
this.hide();
完全不同,除非this
已经是一个 jQuery 对象。标准 DOM 对象没有hide
方法,因此this.hide();
通常是一个错误,而$(this).hide();< /code> 隐藏 DOM 对象并转到下一条语句。
最后,我们得到了至少有意义的东西:
click
函数只需要一个回调函数,因此您可以给它一个函数名或匿名函数(即函数“文字”)。同样,您可以说pancakes(11);
或var Eggs = 11;煎饼(11);
。所以是的,您可以使用命名函数作为 jQuery 回调;但是,这两个
element.click
调用不等效。第一种情况:notify
中的this
的值为window
;在第二种情况下:this
将是notify
中元素的 DOM 对象。也许这不是一个答案,但我需要比评论更多的空间来指出你的问题中的所有困惑。
Um, no you can't. If the second argument to
hide
is a string then it should be an easing and"$(this).show();"
is not an easing; so$("img").hide( 800, "$(this).show();" );
will just put incomprehensible errors into your console and won't even hide anything.If you're seeing this:
used a lot then you're looking at a lot of useless code and you should start looking at better code.
$(this).hide;
doesn't do anything useful, perhaps it will exercise your JavaScript engine's dead code removal algorithms but it certainly won't hide anything on the page. Saying$(this).hide;
is no more productive than saying42;
; sure, they're both legal statements but they don't do anything useful.Perhaps you meant:
But that is totally different than
this.hide();
unlessthis
is already a jQuery object. Standard DOM objects do not havehide
methods sothis.hide();
is, in general, an error whereas$(this).hide();
hides a DOM object and moves on to the next statement.And finally, we get to something that at least makes sense:
The
click
function just needs a callback function so you can give it a function name or anonymous function (i.e. a function "literal"). Similarly, you can saypancakes(11);
orvar eggs = 11; pancakes(11);
.So yes, you can use a named function as a jQuery callback; but, those two
element.click
calls are not equivalent. In the first case:The value of
this
insidenotify
will bewindow
; in the second case:this
will be element's DOM object insidenotify
.Maybe this isn't much of an answer but I needed more space than a comment offers to point out all the confusion in your question.
$(this).hide;
实际上不会做任何事情,我假设你的意思是
$(this).hide()
?this.hide();
您需要更深入地了解 jQuery 的工作原理以及 DOM 的工作原理。在大多数情况下,$(this)
将this
的值(原生 DOM 元素)包装到 jQuery 对象中。普通 DOM 元素没有 hide 方法。例如,您无法执行
document.getElementsByTagName('body')[0].hide()
因为 DOM body 元素没有 hide 方法。 jQuery 将元素包装到一个 jQuery 对象中,该对象本质上是一个包含许多很酷方法的数组,例如hide()
$("this").hide;
最后我认为您需要更深入地了解 Javascript 的工作原理。this
关键字很特殊,与字符串"this"
无关。这是一个基于被误解的原则的模糊问题,我认为这是一个很好的例子,说明为什么你应该专注于学习 Javascript,而不是 jQuery。
$(this).hide;
Won't actually do anything, I assume you mean
$(this).hide()
?this.hide();
You need a deeper understanding of how jQuery works, and how the DOM works.$(this)
in most situations wraps the value ofthis
, a native DOM element into a jQuery object. Normal DOM Elements don't have a hide method.For example, you couldn't do
document.getElementsByTagName('body')[0].hide()
because the DOM body element does not have a hide method. jQuery wraps the element into a jQuery object, which is essentially an array with lots of cool methods, likehide()
$("this").hide;
And finally I think you need a deeper understanding of how Javascript works. Thethis
keyword is special, and not related to the string"this"
.This is a vague question based on misunderstood principles, and I think a very good example of why you should focus on learning Javascript, not jQuery.
好吧,实际上,这两个是不同的:
第一个将在经过该行时执行函数通知(使用调试器检查)。它还将执行显而易见的操作 - 附加单击事件。第二个函数在将该函数附加到单击事件时不会调用通知。
至于其他差异 - 与其说是 jQuery 差异,不如说是 Javascript 语法差异。这里存在相当多的不一致之处 - 其中很多来自浏览器支持以及它们如何实际实现官方 ECMAscript、DOM 和其他扩展。
Well, actually, these two are different:
The first one will execute function notify upon going through that line (check with debugger). It will also do the obvious - attach a click event. The second one does not call notify upon attaching that function to the click event.
As for the other differences - they are not so much jQuery differences, but Javascript syntax differences. There's quite a bit of inconsistencies here are there - a lot of it coming from browser support and how they actually implement the official ECMAscript, DOM and other extensions.