jquery变量,带$前缀和不带$前缀的区别
我读过另一篇关于 jquery 变量的帖子/问题,它很有用,但我仍然无法理解...... 下面是我正在修改的简单选项卡式内容功能的一些代码。
请有人解释一下为什么会这样:
$tabMenuItem.click(function() {
var activetab = $(this).find('a').attr('href');
$(activetab).show(); //show the active ID content
return false;
});
..以及为什么会抛出“$activetab.show is not a function”错误:
$tabMenuItem.click(function() {
var $activetab = $(this).find('a').attr('href');
$activetab.show(); //show the active ID content
return false;
});
据我所知,两种方式都显示变量保存正确的值 - 只有第二个版本会赢”不让我附加一个 jquery 方法/函数...?
我不是一个强大的程序员,任何澄清将不胜感激!
I have read another post/question regarding jquery variables and it was useful but I'm still having trouble understanding...
Below is a bit of code from a simple tabbed content functionality that I'm modifying.
Please could someone explain why this works:
$tabMenuItem.click(function() {
var activetab = $(this).find('a').attr('href');
$(activetab).show(); //show the active ID content
return false;
});
..and why this throws a '$activetab.show is not a function' error:
$tabMenuItem.click(function() {
var $activetab = $(this).find('a').attr('href');
$activetab.show(); //show the active ID content
return false;
});
From what I can gather, both ways show the variable to hold the correct value - only the second version won't let me attach a jquery method/function to it...?
I'm not a strong programmer, and any clarification would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
activetab
是所选锚标记的href
。show()
是一个 jQuery 方法。该方法需要由 jQuery 对象调用。$(activetab)
是一个 jQuery 对象。尽管可能不是你想要的。如果您希望第二个工作并使用
$
为变量添加前缀,您仍然需要将其设为 jQuery 对象。您可以通过$($activetab).show()
来执行此操作。activetab
is thehref
of the selected anchor tag.show()
is a jQuery method. The method needs to be called by a jQuery object.$(activetab)
is a jQuery object. Albeit probably not what you want.If you wanted the second to work and using
$
to prefix your variables, you still need to make it a jQuery object. You can do so by$($activetab).show()
.我认为你在问两个问题。首先是 $ 和 without 之间的区别。大多数开发人员在变量前面使用 $,因为稍后他们知道这是一个 jquery 对象。
第二个问题,为什么该代码不起作用。这是因为你正在执行 attr('href')。它返回属性值。您无法显示,因为 show() 是一个 jquery 函数,可以取消隐藏 html 中的 div。我认为你想要的是alert或console.log。
I think you are asking two questions. First the difference between $ and without. Most developers use $ in front of a variable because later they know that this is a jquery object.
Second question, why that code doesn't work. It's because you are doing attr('href'). which returns the attribute value. You can't show because the show() is a jquery function that unhides a div in html. I think what you want is alert or console.log.
我喜欢将 jQuery 视为“包装器”,也许这不是该术语的最佳用法,但让我解释一下:
当您放置诸如
$(someobj)
之类的内容时,您实际上是在向该对象添加 jquery 功能;你正在将 jQuery“包裹”在它周围。一旦执行此操作,您就可以访问该对象的所有 jQuery 功能。然而,由于您已经“包装”了它,因此该对象不再与您开始时的对象相同。为了跟踪您是否使用 jQuery“包装”对象,您可以在变量名的开头添加 $,以便稍后在阅读代码时,您会知道该对象具有(或没有)jQuery 功能。注意:如果您想从 jQuery 对象获取原始对象,可以使用
$myjqueryobj.get()
。在您的示例中,您将返回单击的项目中第一个 a 标签的 href 值。这是一个字符串,将其传递到 jQuery 对象中,如下所示:
$(activetab)
将 href 值视为选择器(但事实并非如此)。您可以使用alert(activetab);
测试它的值,您将看到它不是一个 jQuery 对象,而是一个字符串。两种方法的工作方式相同,因为 JavaScript 不关心变量是否以 $ 开头。仅供程序员参考。
I like to think of jQuery as a "wrapper", perhaps it's not the best use of the term, but let me explain:
When you put something like
$(someobj)
, you are essentially adding jquery functionality to that object; you are "wrapping" jQuery around it. Once you do this, you have access to all the jQuery functionality for that object. However, since you've "wrapped" it, that object is no longer the same as the one you started from. In order to keep track of whether or not you're using a jQuery "wrapped" object, you can add a $ to the beginning of the variable name so that later on, when you read code, you'll know that that object has (or doesn't have) jQuery functionality.Note: If you want to get the original object back from the jQuery object you can use
$myjqueryobj.get()
.In your examples, you're returning the href value for the first a tag within the clicked item. This is a string and passing it into the jQuery object like this:
$(activetab)
treats the href value as if it was a selector (which it's not). You can test what its value is by usingalert(activetab);
and you will see that it's not a jQuery object, it's a string.Both methods work the same way because JavaScript doesn't care if a variable starts with $ or not. It's only there for the programmer's reference.
在变量前面使用 $ 符号是 JavaScript 程序员用来识别它是 jQuery 对象的命名约定。这是一篇好文章,介绍了一些常见问题命名约定(关于你的问题,请参阅#3)。对于中级程序员来说,这也是一个很好的复习。
在第一个示例中,activetab 是一个 jquery 对象集合。执行 $(activetab) 是多余的,实际上是从 jQuery 对象创建 jQuery 对象。您可以简单地执行 activetab.show();
Using the $ sign in front of a variable is a naming convention that JavaScript programmers use to identify that it is a jQuery object. Here's a good article going over some common naming conventions (Look at #3 regarding your question). Also its a good refresher for the intermediate programmer.
In your 1st example, activetab is a jquery object collection. Doing $(activetab) is redundant and is actually creating a jQuery object out of a jQuery object. You could simply do activetab.show();