问题描述
我有这个简单的鬼文本实现:
I have this simple ghost text implementation:
HTML code:
HTML code:
<div id="searchPanel">
<form method="get" id="searchBox" action="somePage.php">
<input class="ghText" type="text" name="query" value="search here"/>
</form>
</div>
jQuery代码:
$(document).ready(function(){
$txtField = "#searchPanel form input.ghText";
var value = $($txtField).val();
$($txtField).focus(function(){
if($(this).val() == value)
$(this).val("").removeClass("ghText");
});
$($txtField).blur(function(){
if($(this).val()==""){
$(this).val(value).addClass("ghText");
}
});
});
上面的示例不起作用。当用户将光标聚焦在搜索栏上时,由于某种原因,类ghText不会被删除。
The example above is not going to work. When the user focuses the cursor on the search bar, the class "ghText" wont be removed for some reason.
但是现在我改变了var value(变量)初始化)和值与
$ value如下:
However now if I change the "var value" (variable initialization) and "value" with"$value" as in:
$value = $($txtField).val();
$(this).val($value).removeClass("ghText");
$(this).val($value).addClass("ghText");
一切都很完美。
我可以去睡觉而不是太担心它..但我很好奇为什么会发生这样的事情?
I can just go to sleep and not worried too much about it..but I am very curious why something like that can happen?
是因为this没有引用正确的对象,还是因为我试图将jQuery对象存储在非jQuery变量中,或者是否是其它的东西..有人能指出我出了什么问题吗?我一直认为var x与$ x相同..?
is it because of the "this" not referreing to the right object, or is it because i tried storing jQuery object in non-jQuery variable or is it about something else..can somebody point out to me what was wrong? I have always thought that "var x" is the same as "$x"..?
推荐答案
你似乎很困惑关于JavaScript变量。没有jQuery变量和非jQuery变量之类的东西。一些特定情况:
You seem to be confused about JavaScript variables. There is no such thing as "jQuery variables" and "non-jQuery variables". Some specific cases:
- 用 var 声明的变量与不带变量的变量不同。 var x是 local 变量,因此它不会与其他函数共享一个值,这些函数也有一个名为x的变量。这几乎总是一件好事,所以你几乎总是用var声明变量。
- jQuery中的$有点特殊。 特殊;只是jQuery声明了一个名为$的变量,它做了一些奇特的操作。
- 对于以$开头的变量没有什么特别之处。换句话说,$ x只是一个变量名。它是x的不同变量,它不是jQuery变量。它只是一个名为$ x的JavaScript变量。 (这与PHP不同,其中$实际上是一种特殊的变量语法。)
- A variable declared with var is different to a variable without. "var x" is a local variable, so it will not share a value with other functions which also have a variable called "x". This is almost always a good thing, so you should almost always declare variables with "var".
- The $ in jQuery is sort of special. It isn't that special; it's just that jQuery has declared a variable called "$" which does some fancy operations.
- There is nothing special about variables that begin with "$". In other words, "$x" is just a variable name. It is a different variable to "x", and it isn't a "jQuery variable". It's just a JavaScript variable called "$x". (This is different from PHP, where the $ is actually a special variable syntax.)
所以你可以称它为值而不是$ value。
So you can just call it "value" instead of "$value".
您删除var的事实可能会将其变为全局变量。
Possibly the fact that you removed the "var" changed things by making it into a global variable.
对于this,是的,这是JavaScript的棘手问题,可能会导致您的问题。内部焦点和模糊功能中this的值可能与this外部的值不同。我不确定事件处理程序中this指的是什么,但它不会是同一个对象。所以你可能想做的是将this分配给外部函数中的变量,然后在内部引用该变量代替this。
As for "this", yes, that is a tricky aspect of JavaScript, and might be causing your problem. The value of "this" inside the inner 'focus' and 'blur' functions is likely to be different from the value of "this" outside. I'm not sure exactly what "this" refers to in an event handler, but it will not be the same object. So what you probably want to do is assign "this" to a variable in the outer function, and then refer to that variable on the inside in place of "this".
这篇关于混淆简单变量声明jQuery“$ variable” vs javascript" var"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!