可以将一个变量的值从一个javascript导出到另一个? [英] Is it possible to export a value of an variable from one javascript to an other?
问题描述
我该如何做?
这里的脚本
menu_bar.js
$(document).ready(function(){
函数wrapper_action(id_menu_bar){
$(。wrapper)。animate({height:0px );
$(#changer p)。click(function(){
$(。wrapper)。animate({height:300px});
}) ;
}
$(#select_place li)。live(click,function(){
var wrapper_id = $(。wrapper)。attr id);
var id_place = this.id;
if(wrapper_id!=place)
{
$(#select_level li 。();
$(#select_building)。load(menu_bar / menu_bar_building.php?placeitem =+ id_place,function(){
$(#select_building li (function(){
var id_building = this.id;
if(wrapper_id!=building)
{
$(# select(select_level)。load(menu_bar / menu_bar_level.php?builditem =+ id_building,function(){
$(#select_level li)click(function(){
var id_level = this 。ID;
wrapper_action(id_level);
});
});
}
else if(wrapper_id ==building)
{wrapper_action(id_building);}
});
});
}
else if(wrapper_id ==place)
{wrapper_action(id_place);}
});
});
如果变量 id_menu_bar
在全局范围内,可以由页面上的其他脚本使用。
jQuery的 $ .data()
也适用于存储针对元素的数据,意味着您不需要使用全局变量并污染全局命名空间。
编辑:
为了回应您的评论,您如何声明变量来确定它们是如何在JavaScript范围内。
全局变量
在声明如
var myVariable;
或
MYVARIABLE;
将没有区别 - 两个变量都将具有全局范围。事实上,第二种方法将给出一个可变的全局范围,甚至在函数内部。例如
function firstFunction(){
//本地作用域,范围为firstFunction
var localVariable;
//全局范围,即可在页面中运行
//的所有其他JavaScript代码
globalVariable =我不是真的隐藏;
}
函数secondFunction(){
//我可以在这里访问globalVariable,但只有在
// firstFunction已执行后
alert(globalVariable) ; //警报我不是真的隐藏
}
这种情况的区别是执行 secondFunction()
直到为止,警报将失败,不显示
已被执行,因为这是变量被声明的地方。如果变量被声明在任何函数之外,则该警报将成功并显示 globalVariable
的值firstFunction() globalVariable
使用jQuery.data()
使用此命令可以将数据存储在元素的缓存对象中。我会建议看看来源,看看这是如何实现的,但它是非常整洁的。考虑
function firstFunction(){
$ .data(document,myVariable,我不是真的隐藏);
globalVariable =我不隐藏;
}
函数secondFunction(){
//警报我不是真的隐藏,但只有在
// secondFunction $ b $之前执行firstFunction b alert($。data(document,myVariable));
//警报我不隐藏,但只有在
// secondFunction
alert(globalVariable)之前执行firstFunction;
}
在这种情况下,字符串值I不真正隐藏
使用 firstFunction
myVariable 存储在文档对象中>。然后可以从脚本中的其他任何地方的缓存对象检索该值。尝试从缓存对象中读取一个值,而不必首先设置它将生成 undefined
。
看看这个 工作演示 。
为了不使用全局变量,请查看这个文章。
I have made a Web page using jquery and php where all files are used in a modular style. Now I have two JavaScript files which must communicate with each other. One Script generates a variable (id_menu_bar) which contains a number. I want that this variable gets transported to the second JavaScript and is used there.
How do I make that?
Here the Script
menu_bar.js
$(document).ready(function() {
function wrapper_action(id_menu_bar) {
$(".wrapper").animate({height: "0px"});
$("#changer p").click(function() {
$(".wrapper").animate({height: "300px"});
});
}
$("#select_place li").live("click", function() {
var wrapper_id = $(".wrapper").attr("id");
var id_place = this.id;
if (wrapper_id != "place")
{
$("#select_level li").remove();
$("#select_building").load("menu_bar/menu_bar_building.php?placeitem="+id_place, function() {
$("#select_building li").click(function() {
var id_building = this.id;
if (wrapper_id != "building")
{
$("#select_level").load("menu_bar/menu_bar_level.php?buildingitem="+id_building, function() {
$("#select_level li").click(function() {
var id_level = this.id;
wrapper_action(id_level);
});
});
}
else if (wrapper_id == "building")
{wrapper_action(id_building);}
});
});
}
else if (wrapper_id == "place")
{wrapper_action(id_place);}
});
});
if the variable id_menu_bar
is in global scope then it can be used by another script on the page.
jQuery's $.data()
is also good for storing data against elements and means that you do not need to use a global variable and pollute the global namespace.
EDIT:
In response to your comment, there is a difference in how you declare variables that determines how they are scoped in JavaScript.
Global Variables
Outside of a function declaring a variable like
var myVariable;
or
myVariable;
will make no difference - both variables will have global scope. In fact, the second approach will give a variable global scope, even inside of a function. For example
function firstFunction() {
// Local scope i.e. scoped to firstFunction
var localVariable;
// Global scope i.e. available to all other JavaScript code running
// in the page
globalVariable = "I'm not really hiding";
}
function secondFunction() {
// I can access globalVariable here but only after
// firstFunction has been executed
alert(globalVariable); // alerts I'm not really hiding
}
The difference in this scenario is that the alert will fail and not show the value for globalVariable
upon execution of secondFunction()
until firstFunction()
has been executed, since this is where the variable is declared. Had the variable been declared outside of any function, the alert would have succeeded and shown the value of globalVariable
Using jQuery.data()
Using this command, you can store data in a cache object for an element. I would recommend looking at the source to see how this achieved, but it is pretty neat. Consider
function firstFunction() {
$.data(document,"myVariable","I'm not really hiding");
globalVariable = "I'm not hiding";
}
function secondFunction() {
// alerts "I'm not really hiding" but only if firstFunction is executed before
// secondFunction
alert($.data(document, "myVariable"));
// alerts "I'm not hiding" but only if firstFunction is executed before
// secondFunction
alert(globalVariable);
}
in this scenario, a string value "I'm not really hiding"
is stored against the document object using the key string myVariable
in firstFunction
. This value can then be retrieved from the cache object anywhere else in the script. Attempting to read a value from the cache object without having first set it will yield undefined
.
Take a look at this Working Demo for more details.
For reasons not to use Global Variables, check out this article.
这篇关于可以将一个变量的值从一个javascript导出到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!