$ variable and javascript/jquery question
I recently solved a jquery problem I had by switching all the '$'s in my code to 'jQuery'
I'm trying to use this code:
var $items = $('#vtab>ul>li');
$items.mouseover(function() {
$items.removeClass('selected');
$(this).addClass('selected');
var index = $items.index($(this));
$('#vtab>div').hide().eq(index).show();
}).eq(1).mouseover();
but am not sure how to change the $ signs out for $items. Is it as simple as jQuery.item开发者_如何学JAVAs? Is this even jQuery code? I'm confused.
Thanks, Zeem
$items
does not need to be changed. jQuery's variable is (typically) just a $
, whereas $items
is it's own variable name.
So your code would become:
var $items = jQuery('#vtab>ul>li');
$items.mouseover(function() {
$items.removeClass('selected');
jQuery(this).addClass('selected');
var index = $items.index(jQuery(this));
jQuery('#vtab>div').hide().eq(index).show();
}).eq(1).mouseover();
Edit You could also use a self-invoking function like so:
(function($){
var $items = $('#vtab>ul>li');
$items.mouseover(function() {
$items.removeClass('selected');
$(this).addClass('selected');
var index = $items.index($(this));
$('#vtab>div').hide().eq(index).show();
}).eq(1).mouseover();
})(jQuery);
This will allow you to use the $
variable in your code BUT you will not be able to use the variables within, outside of the function so be careful.
You don't need to replace $items at all.
$items = jQuery('#vtab>ul>li');
will work.
$items is a variable name and is not part of the $ namespace.
var $items
is just a variable. Prefixing variables with $
lets the developer know that it's a jQuery object.
精彩评论