简体   繁体   English

如何将一个函数的值转换为文档准备就绪?

[英]How can I get the value from one function into document ready?

I have 2 files. 我有2个档案。 File one calls a function in file 2. I can alert the value passed into file 2 if that function is outside of the document ready function. 文件1调用文件2中的一个函数。如果该函数超出文档就绪函数的范围,我可以警告传递给文件2的值。 I need that value inside of document ready. 我需要在文档中准备好该值。 Is there a way to do this? 有没有办法做到这一点?

file 1: 文件1:

setId(1);

file 2 文件2

function setId(v){
  alert(v); // This works
}

If I place the function within the document ready function, I can no longer access the value passed in to it. 如果将函数放在文档就绪函数中,则无法再访问传递给它的值。

use the following code define setId function 使用以下代码定义setId函数

window.setId = function(v){
alert(v);
}

EDIT: Method 2 from Nick 编辑:尼克的方法2

window.myapp = window.myapp || {};

myapp.setId = function(v){
alert(v);
}

to call the setId function 调用setId函数

myapp.setId("test");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从不在$ {document).ready(JS)中的函数中获取输入值 - Get value from input in function that not in $(document).ready(JS) 为什么我不能进入我的$ document Ready函数 - Why can't I get to my $document Ready function 如何在CasperJS中的document.ready(function(){})之后获取文本区域的值 - How to get value of a textarea after document.ready(function(){ }) in CasperJS 如何获取设置在$(document).ready函数上的字段的值? - how to get value of a field which set on $(document).ready function? 文档准备好后如何获取自动填充输入的值? - How can I get value of auto filled input when document is ready? 是否可以在$(document).ready(){})中获得普通函数的值? - Is it possible to get the value of normal function in $(document).ready(){}); 如何在$(document).ready(function(){})中使用for循环? - How can I use a for-loop within a $(document).ready(function(){})? 如何将相同的$(document).ready(function()用于多个输入? - How can I use the same $(document).ready(function() for multiple inputs? 如何访问iframe的$(document).ready(function(){})中包含的函数? - How can I access the functions wrapped in the $(document).ready(function(){}) of an iframe? 如何在document.ready的最后调用函数 - How can I call a function at the very end of document.ready
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM