如何从常规 javascript 函数访问 jquery 中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6620993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:18:40  来源:igfitidea点击:

how to access variable inside jquery from regular javascript function

javascript

提问by user826323

I am a newbie to the jquery. I am trying to access a variable defined inside jquery block outside the jquery (from regular function) like this, but I can't access it. Could somebody tell me how?

我是 jquery 的新手。我正在尝试像这样访问 jquery 外部 jquery 块中定义的变量(来自常规函数),但我无法访问它。有人能告诉我怎么做吗?

<script language="javascript">
$(function()
{
    .......
    .......
    .......
    var api = pane.data('jsp');
    var current_location = api.getContentPositionX();
}

function change_title(t_index) {
    alert("print="+$.current_location);
    window.location.href="page.php?p="+$.current_location);
}

I want to get a value for $.current_location.

我想获得 $.current_location 的值。

Thanks.

谢谢。

回答by Guffa

There is no such thing as a "jQuery variable", they are all regular Javascript varaibles.

没有“jQuery 变量”这样的东西,它们都是常规的 Javascript 变量。

The reason that you can't access the current_locationvariable from your function is that the variable is declared locally inside another function.

您无法current_location从函数访问该变量的原因是该变量是在另一个函数中本地声明的。

Just declare the variable outside the functions so that it's global, and you can access it from both functions:

只需在函数外部声明变量,使其成为全局变量,您就可以从两个函数访问它:

var current_location;

$(function() {
    .......
    .......
    .......
    var api = pane.data('jsp');
    current_location = api.getContentPositionX();
}

function change_title(t_index) {
    alert("print=" + current_location);
    window.location.href = "page.php?p=" + current_location;
}

回答by ddinchev

The jQuery object is global, as long as you have included jQuery on the page of course. So calling $.property or jQuery.property ($ is alias) should work as long as the property exists, is set and is public.

jQuery 对象是全局的,当然,只要您在页面中包含了 jQuery。所以调用 $.property 或 jQuery.property($ 是别名)应该可以工作,只要属性存在,设置并且是公共的。

You might want to do in order to make the current_location var a member of the jQuery object:

为了使 current_location 变量成为 jQuery 对象的成员,您可能想要这样做:

$(function()
{
    .......
    .......
    .......
    var api = pane.data('jsp');
    this.current_location = api.getContentPositionX();
}

回答by Jim Deville

you need to store current_location on a globally accessible object. either window, or a custom object that you use as a namespaced object.

您需要将 current_location 存储在一个全局可访问的对象上。要么window,或自定义对象,您作为一个命名空间对象使用。

<script language="javascript">
var current_location = null;
var obj = {};
$(function() {
    .......
    .......
    .......
    var api = pane.data('jsp');
    current_location = api.getContentPositionX();
    obj.current_location = current_location;
})

function change_title(t_index) {
    obj.current_location;
    alert("print="+$.current_location);
    window.location.href="page.php?p="+current_location);
}

回答by tkerwood

The variable is defined within the first function, so the scope of that variable is only within the first function and hence cannot be seen from outside that function.

该变量是在第一个函数中定义的,因此该变量的范围仅在第一个函数内,因此从该函数外部无法看到。

Try defining the variable outside the function so it has a bigger scope.

尝试在函数外部定义变量,使其具有更大的作用域。