【发布时间】:2012-02-22 15:19:01
【问题描述】:
我不知道为什么我不能将 .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 函数内)工作正常,输出some simple text..,第二个什么也不输出。
请帮忙。
谢谢大家这么快的回复。你们都对 .hover 功能是绝对正确的。我的错。但现在我有另一个相关的问题。 我需要这个值将它传递给像这样调用的“qTip”插件:
$("#cartItems tr.cItem").qtip(
{
content: receipt,
show: 'mouseover',
hide: 'mouseout'
});
我应该以某种方式合并这个调用吗?
【问题讨论】:
-
第二个console.log只会运行一次,当封闭函数被执行时,没有任何东西被分配给receipt。
标签: javascript jquery html variables scope