我如何使用另一个file.js中的jQuery

时间:2022-06-22 05:07:29

I have a HTML file:

我有一个HTML文件:

<!DOCTYPE html>
<html>
<head>
<title>Insert</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script type="text/javascript" src="js/jquery-1.10.1.js"></script>
<script type="text/javascript" src="js/controlla.js"></script>
</head>
<body>
<div id="insert">
<img src="css/images/logo.png" alt="Search Engine Logo" />
<input id="#textboxone" onkeyup="scova(event)" type="text"/>
</div>
</body>
</html>

I have inserted script reference in order (first jquery, second my function file). In my function file, there is function scova(event) from onkeyup event in inputbox, is not possible use jQuery reference $.

我按顺序插入了脚本引用(第一个jquery,第二个我的函数文件)。在我的函数文件中,输入框中的onkeyup事件有函数scova(事件),不可能使用jQuery引用$。

For example:

例如:

function scova(e) {
var valore = document.getElementById('#textboxone').value;
var valjquiro = $('#textboxone').val();
console.log('il valore di javascript: ' + valore);
console.log('il valore di jaquirro: '+valjquiro);
};

And in the browser console (after the digit 'test') I get:

在浏览器控制台(数字'测试'之后),我得到:

il valore di javascript: test
il valore di jaquirro: undefined 

How can I use JQuery in my function file? How to use $ reference of jquery in my controlla.js?

如何在我的函数文件中使用JQuery?如何在我的controlla.js中使用jquery的$引用?

4 个解决方案

#1


8  

Change

更改

<input id="#textboxone" onkeyup="scova(event)" type="text"/>

to

<input id="textboxone" onkeyup="scova(event)" type="text"/>

so that $('#textboxone') won't be an empty object.

这样$('#textboxone')就不会是一个空对象。

Once you've done that, fix also document.getElementById('#textboxone') by removing the #.

完成后,通过删除#来修复document.getElementById('#textboxone')。

#2


2  

either remove # from your id attribute OR do this -

从您的id属性中删除#或执行此操作 -

var valjquiro = $('#\\#textboxone').val();

#3


1  

You don't need to include jQuery inside any other JavaScript file: just put jQuery on top of all your <script> tags and all the next files imported will have access to the library.

您不需要在任何其他JavaScript文件中包含jQuery:只需将jQuery放在所有

#4


0  

Try wrapping up the code from your function file like this:

尝试从函数文件中包装代码,如下所示:

$(document).ready(function(){ put your code here })

Read more about this here: http://api.jquery.com/ready/

在此处阅读更多相关信息:http://api.jquery.com/ready/

Your code is probably running before the DOM has fully loaded, in its current state, if I understood you correctly.

如果我理解正确的话,你的代码可能在DOM完全加载之前运行,处于当前状态。

#1


8  

Change

更改

<input id="#textboxone" onkeyup="scova(event)" type="text"/>

to

<input id="textboxone" onkeyup="scova(event)" type="text"/>

so that $('#textboxone') won't be an empty object.

这样$('#textboxone')就不会是一个空对象。

Once you've done that, fix also document.getElementById('#textboxone') by removing the #.

完成后,通过删除#来修复document.getElementById('#textboxone')。

#2


2  

either remove # from your id attribute OR do this -

从您的id属性中删除#或执行此操作 -

var valjquiro = $('#\\#textboxone').val();

#3


1  

You don't need to include jQuery inside any other JavaScript file: just put jQuery on top of all your <script> tags and all the next files imported will have access to the library.

您不需要在任何其他JavaScript文件中包含jQuery:只需将jQuery放在所有

#4


0  

Try wrapping up the code from your function file like this:

尝试从函数文件中包装代码,如下所示:

$(document).ready(function(){ put your code here })

Read more about this here: http://api.jquery.com/ready/

在此处阅读更多相关信息:http://api.jquery.com/ready/

Your code is probably running before the DOM has fully loaded, in its current state, if I understood you correctly.

如果我理解正确的话,你的代码可能在DOM完全加载之前运行,处于当前状态。