简体   繁体   English

在jquery脚本中使用变量

[英]using variable in jquery script

i imported my script 我导入了我的脚本

<script src="/test.js"></script>

and the content is 而且内容是

var test= $(document).find('aside#bar');
var list= test.find('ul');

then i using following script to append string 然后我使用以下脚本来追加字符串

<script>
$(document).ready(function () {
  $(list).text("testing message");
});
</script>

but chrome console says Uncaught ReferenceError: list is not defined 但Chrome控制台说Uncaught ReferenceError: list is not defined

how can i use variable in jquery? 我怎样才能在jquery中使用变量?

Update 更新

i discovered all script is not work in that page 我发现所有脚本在该页面中都不起作用

i use 我用

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script src="/test.js"></script>
    <script>

$(document).ready(function () {
  $(document).html("testing message");
});
    </script>

but nothing append,chrome console doesn't log anything 但没有任何附加功能,Chrome控制台不会记录任何内容

In your test.js file you declare a local variable. 在test.js文件中,声明一个局部变量。 You have to declare it global using window.variableName like bellow: 你必须使用window.variableName声明它是全局的,如下:

window.test= $(document).find('aside#bar');
window.list= test.find('ul');

Then you will be able to access it from other Javascript files that were loaded after test.js. 然后,您将能够从test.js 之后加载的其他Javascript文件访问它。

  1. Check if /test.js is being loaded in Chrome. 检查是否在Chrome中加载了/test.js. I think the URL you are giving to this file might be wrong 我认为你给这个文件的URL可能是错的

     <script src="/test.js"> 

    Open Chrome's Network panel , and reload the page. 打开Chrome的“网络”面板 ,然后重新加载页面。 Are you seeing a Red error message when the test.js script is trying to load? test.js脚本尝试加载时,您是否看到Red错误消息?

    If this page is, say, http://localhost/test/mypage.html , then src='/test.js' will load http://localhost/test.js . 如果这个页面是http://localhost/test/mypage.html ,那么src='/test.js'将加载http://localhost/test.js Because of the / in the beginning. 因为/在开始。 So check the URL path and see if the script is getting loaded first. 因此,请检查URL路径,看看是否先加载脚本。

  2. test.js script is just saying var test = ..., var list = ... . test.js脚本只是说var test = ..., var list = ... But at this point, the document may not even have loaded! 但此时,文档甚至可能没有加载! But of course, if your script test.js got loaded properly - then it would have shown an error for the test and list variables itself. 但是,当然,如果您的脚本test.js已正确加载 - 那么它将显示test错误并list变量本身。 Since you're not seeing errors when this variables are getting set, my first guess is that test.js is not getting loaded. 由于你在设置这个变量时没有看到错误,我的第一个猜测是test.js 没有被加载。 Fix the script loading first, and then fix the script itself: 首先修复脚本加载,然后修复脚本本身:

     $(document).ready(function() { var test = ... var list = ... }); 
  3. But again, this will also not work when you try to use test or list somewhere else! 但是,当您尝试在其他地方使用testlist时, 这也将无效 Because they are declared using var . 因为它们是使用var声明的。 var creates only local variables, so they won't be available outside the function (if you check above, we're wrapping the var test =..., var list = ... inside a function() { } , so they won't be available outside that function). var只创建局部变量,因此它们在函数外部不可用(如果你在上面检查,我们将var test =..., var list = ...包装在一个function() { } ,所以它们在该功能之外将不可用)。 So make them like: 所以让它们像:

     $(document).ready(function() { window.test = ... window.list = ... }); 

    You can also accomplish the same just by removing the var keyword, but that has its own problems. 您也可以通过删除var关键字来完成相同的操作,但这有其自身的问题。 Make sure you read Javascript Variable Scopes for a start. 确保您首先阅读Javascript Variable Scopes

  4. Use either type or language for all script tags: 对所有脚本标记使用任何类型或语言:

     <script language="javascript" type="text/javascript"> $(document).ready(...) </script> 

just use list.html("test message") or list.text("test message") 只需使用list.html("test message")list.text("test message")

$(document).ready(function () {
  list.html("testing message");
});

DEMO DEMO

EDIT: 编辑:

and because of use jquery function in test.js 并且因为在test.js中使用了jquery函数

<script src="jquery.js"></script>  <= First
<script src="test.js"></script> <= Second

Why you don't define variables when the document is ready?? 为什么在文档准备好时不定义变量?

You can try in the script 你可以尝试在脚本中

$(
   var test= $(document).find('aside#bar');
   var list= test.find('ul');
);

and

<script>
$(document).ready(function () {
  $(list).text("testing message");
});
</script>

But I would do 但我会这样做

$(
       var test= $(document).find('aside#bar');
       var list= test.find('ul');
       $(list).text("testing message");
    );

Finaly.... what is aside#bar ???? 最后......除了#bar ????

aside is a class?? 旁边是一个班级? You forget the dot?? 你忘记了点? .aside?? 。在旁边?? The problem maybe there. 可能存在问题。

Sorry for my bad english and hope this help! 抱歉我的英文不好,希望对你有所帮助! Bye! 再见!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM