为什么不能将函数内部的值赋给外部(全局)声明的变量?
我不知道为什么我不能将 .hover 函数捕获的值分配给全局声明的变量。
这是我的 jQuery 代码:
jQuery(function($){
var receipt;
$("#cartItems tr.cItem").hover(
function()
{
$(this).addClass("highlight");
receipt = $(this).next().children().text();
console.log(receipt);
},
function()
{
$(this).removeClass("highlight");
}
);
console.log(receipt);
});
这是我的 HTML:
<table id="cartItems">
<tr>
<td>Lp.</td><td>z:</td><td>na:</td><td>cena netto:</td>
</tr>
<tr class="cItem">
<td>ru</td><td>pl</td><td>16.00</td>
</tr>
<tr>
<td colspan="4">some simple text that should be assigned </td>
</tr>
</table>
首先 console.log(receipt)
(在 .hover
函数内)工作正常并输出 一些简单的文本。 .
第二个不输出任何内容。
请帮忙。
谢谢大家这么快的回复。你们对 .hover 功能的看法都是绝对正确的。我的错。但现在我有另一个相关的问题。 我需要这个值将其传递给“qTip”插件,如下所示:
$("#cartItems tr.cItem").qtip(
{
content: receipt,
show: 'mouseover',
hide: 'mouseout'
});
我应该以某种方式合并这个调用吗?
I don't know why I can't assign a value captured by .hover function to a variable declared globally.
Here is my jQuery code:
jQuery(function($){
var receipt;
$("#cartItems tr.cItem").hover(
function()
{
$(this).addClass("highlight");
receipt = $(this).next().children().text();
console.log(receipt);
},
function()
{
$(this).removeClass("highlight");
}
);
console.log(receipt);
});
And here is my HTML:
<table id="cartItems">
<tr>
<td>Lp.</td><td>z:</td><td>na:</td><td>cena netto:</td>
</tr>
<tr class="cItem">
<td>ru</td><td>pl</td><td>16.00</td>
</tr>
<tr>
<td colspan="4">some simple text that should be assigned </td>
</tr>
</table>
First console.log(receipt)
(inside .hover
function) works fine and outputs some simple text..
and the second outputs nothing.
Please help.
Thank You All for so quick reply. You All are absolutely right about .hover function. My fault. But now I have another related problem.
I need this value to pass it to "qTip" plugin called like this:
$("#cartItems tr.cItem").qtip(
{
content: receipt,
show: 'mouseover',
hide: 'mouseout'
});
Should I merge somehow this calls ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对 console.log 的“第二次”调用实际上是第一次(当文档准备好时发生),此时您的 var 尚未定义
the "second" call to console.log is actually the first (it occurs when document is ready), and at this moment your var is not defined
该值仅在调用悬停函数后才会分配给
receipt
。您在悬停#cartItems tr.cItem
之前登录到控制台。The value will only be assigned to
receipt
after the hover functions have been called. You're logging to the console before#cartItems tr.cItem
has been hovered.正如其他人提到的,您当前放置
console.log(receipt)
的位置,receipt
尚未初始化。如果您想将其更改为更有意义的内容,您可以:
这是一个 jsfiddle 演示,展示了这一点。
请注意,当它第一次输出值时,它是
未定义
,因为当页面首次加载时,悬停事件尚未触发,因此变量未初始化。将鼠标悬停并单击链接后,它现在有了一个值。As others have mentioned, where you currently have placed the
console.log(receipt)
,receipt
will not yet have been initialized.If you want to change this to something more meaningful you can:
Here's a jsfiddle demo showing this.
Notice when it firsts outputs the value it's
undefined
since when the page first loads the hover event hasn't fired so the variable is uninitialized. After you hover and click the link it now has a value.第二个
console.log
就其在代码中的布局而言,发生在悬停之前,因此该变量是未定义的。悬停后,它会正确记录下一个 tr 子级的文本。这是一个 jsfiddle,你的代码工作正常。 http://jsfiddle.net/63xKR/The second
console.log
as far as how it lays out in the code happens before the hover, thus the variable is undefined. After you hover, it properly logs the text of the next tr's children. Here's a jsfiddle, your code works fine. http://jsfiddle.net/63xKR/只有当您实际将鼠标悬停在元素上时才会执行hover()函数。所以你写的基本上是:
如你所见,没有人真正调用该函数(documentReady()),因此收据将保持为空。
the hover() function is only executed once you actually hover over an element. so what you are writing is basically:
as you can see, nobody is actually calling the function (documentReady()), therefore receipt will stay null.