jQuery 如何创建Jquery外部文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3246166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Jquery external file?
提问by Sergio
I want to create external JS file for the form and post that data using AJAX.
我想为表单创建外部 JS 文件并使用 AJAX 发布该数据。
The simplified HTML looks like:
简化的 HTML 如下所示:
<form action="" id="message" name="message" method="post">
<input name="message_subject" type="text" id="message_subject" class="message_wall" />
<textarea cols="50" rows="5" id="message_text" class="message_wall"></textarea>
<button type="submit" id="mess" class="mess">send</button>
The Jquery that I'm currently using for this form:
我目前用于此表单的 Jquery:
$("form#message").submit(function() {
var message_subject = $(".message_subject").attr('value').replace(/\n/g,"<br/>").replace(/\n\n+/g, '<br /><br />').replace(/(\<\/?)script/g,"noscript");
var message_text= $(".message_text").attr('value').replace(/\n/g,"<br/>").replace(/\n\n+/g, '<br /><br />').replace(/(\<\/?)script/g,"noscript");
$.ajax({
type: "POST",
url: "mess/somefile.php",
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-2",
data: "message_subject="+ message_subject + "&message_text=" + message_text,
success: function(){
$(".message_field").html('Thanks!');
}
});
return false;
});
Can I this Jquery code be put in an external JS file and then called like:
我可以将此 Jquery 代码放入外部 JS 文件中,然后像这样调用:
$(document).ready(function(){
myexternalfunction();
});
回答by Jaison Justus
yes you can...
是的你可以...
you can do it by linking the js file to html like
你可以通过将 js 文件链接到 html 来做到这一点
<html>
<head>
<script>
//.........
// script calling some external js func
//.........
</script>
<body>
<!-- other tags -->
<script src="test.js" type="text/javascript"></script>
</body>
</html>
//test.js
$(document).ready(function() {
$('#arrow img').click(function() {
transferEmp('test');
});
});
function transferEmp(postData) {
$.ajax({
url: url,
type: 'POST',
data: postData,
success: function(msg){
//alert(msg);
}
});
}
回答by Kris.Mitchell
I would wrap your jQuery code in the $(document).ready(function() code throw all of the jquery code into a .js file, then use standard html to include it.
我会将您的 jQuery 代码包装在 $(document).ready(function() 代码中,将所有 jquery 代码放入一个 .js 文件中,然后使用标准 html 将其包含在内。
When the page is loaded by the browser, your JQuery code will be included as well.
当浏览器加载页面时,您的 JQuery 代码也将包含在内。
回答by Otar
Consider this:
考虑一下:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="common.js"></script>
And the common.js like this:
和 common.js 这样的:
function initialize()
{
// Your jQuery code goes here
}
// Now you have to call this code
$(initialize);
回答by Jan
You can use jQuery.getScript method. http://api.jquery.com/jQuery.getScript/
您可以使用 jQuery.getScript 方法。 http://api.jquery.com/jQuery.getScript/