为什么不能将函数内部的值赋给外部(全局)声明的变量?

发布于 2025-01-08 01:26:12 字数 1291 浏览 0 评论 0原文

我不知道为什么我不能将 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

老街孤人 2025-01-15 01:26:12

对 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

墨小沫ゞ 2025-01-15 01:26:12

该值仅在调用悬停函数后才会分配给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.

蓝海似她心 2025-01-15 01:26:12

正如其他人提到的,您当前放置 console.log(receipt) 的位置,receipt 尚未初始化。

如果您想将其更改为更有意义的内容,您可以:

  1. 将变量声明移至加载函数之外
  2. 在其自己的函数中调用 console.log(receipt) ,您可以将其附加到单击事件或之后的任何其他操作 加载函数。
var 收据;

jQuery(函数($){
    $("#cartItems tr.cItem").hover(

    功能 () {
        $(this).addClass("突出显示");
        收据 = $(this).next().children().text();
        console.log(收据);
    }, 功能 () {
        $(this).removeClass("突出显示");
    });

});

函数 printConsole() {
    console.log(收据);
}

这是一个 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:

  1. Move your variable declaration outside of the on load function
  2. Call console.log(receipt) in its own function which you can attach to a click event or any other action after the load function.
var receipt;

jQuery(function ($) {
    $("#cartItems tr.cItem").hover(

    function () {
        $(this).addClass("highlight");
        receipt = $(this).next().children().text();
        console.log(receipt);
    }, function () {
        $(this).removeClass("highlight");
    });

});

function printConsole() {
    console.log(receipt);
}

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.

老娘不死你永远是小三 2025-01-15 01:26:12

第二个 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/

遇见了你 2025-01-15 01:26:12

只有当您实际将鼠标悬停在元素上时才会执行hover()函数。所以你写的基本上是:

var receipt = null;
var documentReady = someFunctionToBeExecutedLater(){ doesnt matter whats in here };
console.log(receipt);

如你所见,没有人真正调用该函数(documentReady()),因此收据将保持为空。

the hover() function is only executed once you actually hover over an element. so what you are writing is basically:

var receipt = null;
var documentReady = someFunctionToBeExecutedLater(){ doesnt matter whats in here };
console.log(receipt);

as you can see, nobody is actually calling the function (documentReady()), therefore receipt will stay null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文