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

use*_*323 5 javascript

我是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);
}
Run Code Online (Sandbox Code Playgroud)

我想获得$ .current_location的值.

谢谢.

Guf*_*ffa 7

没有"jQuery变量"这样的东西,它们都是常规的Javascript变量.

您无法current_location从函数访问变量的原因是该变量在另一个函数内部声明.

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

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;
}
Run Code Online (Sandbox Code Playgroud)