在 .js 页面中包含 Jquery 脚本

发布于 2024-10-05 00:00:27 字数 941 浏览 4 评论 0原文

抱歉,如果标题不太清楚,但我认为这是正确的。 NEhow,我想做的有点像(在某种程度上)使用 JQuery(首选)、PHP 和 PHP 构建一个小部件。 CSS。

我真正希望发生的是让我的网站的“成员”只需在其 HTML 中粘贴 2 行代码即可加载小部件。像这样的东西:

<script type="text/javascript" src="http://www.mydomain.com/script.js"></script>

然后显示像这样的小部件

好吧,这一点“简单”,没问题。但是我如何包含 JQuery 或“某些东西”来在 script.js 中生成小部件

我的意思是“displaywidget” - 小部件 div 的 ID 将是我的服务器上的 php 文件的名称,所以本质上 script.js 需要将 displaywidget.php 加载到 div displaywidget 中。

我想我使用 document.getElementById('displaywidget') 来获取 div,但是如何在 div 内“写入/插入/加载”displaywidget.php?

当我写“纯”java时,我认为可以做“我想要的大部分事情,即 document.getElementById('displaywidget'),但我更愿意“包含”Jquery.js,因为我想要一些使用 JQuery 的小部件的各个方面。示例是 JQuery UI 日期函数。

抱歉,如果我有点胡言乱语,但我正在尝试思考,我的“真正”问题是我不太确定“纯”javascript。显示/加载 displaywidget.php 的 div

请提出建议(哦,如果我找错了树,请随时告诉我 - 很好:))

提前致谢。

Sorry if title is not too clear but I think it's about right. NEhow, what I would like to do is a bit like (well is to a certain extent) building a widget with JQuery (pref), PHP & CSS.

What I would really like to happen is for a "member" of my site to simply paste 2 lines of code in their HTML to load the widget. Something like this:

<script type="text/javascript" src="http://www.mydomain.com/script.js"></script>

Then to display the widget something like this <div id="displaywidget"></div>

OK that bit is "easy" and ok. But how do I include JQuery or "something" to generate the widget in script.js

What I mean is "displaywidget" - the ID of the widget div will be the name of a php file on my server so essentially script.js will need to load displaywidget.php into the div displaywidget.

I think I use document.getElementById('displaywidget') to get the div but how do I then "write/insert/load" displaywidget.php inside the div?

Thinking as I write "pure" java can do "most of what I want i.e. document.getElementById('displaywidget'), BUT I would prefer to also "include" Jquery.js as I would like some aspects of the widget to use JQuery. Example being the JQuery UI date function.

Sorry if I am rambling a bit but trying to think as I go along. My "real" problem is I am not too sure on "pure" javascript i.e. getting the div to display/load displaywidget.php

Suggestions please. (Oh if I am barking up the wrong tree please feel free to tell me - nicely:) )

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您的好友蓝忘机已上羡 2024-10-12 00:00:27

但是如何在 div 内“写入/插入/加载”displaywidget.php?

我想我使用 document.getElementById('displaywidget') 来获取 div , 在 jQuery 内部,它将调用 php 页面,然后将数据推送到 div 中。

您应该在此过程的早期加载 jQuery,就在 head 元素的前面。一旦加载,它就会被缓存,所以不用担心它在每个页面上。没有产生实际的开销。

安装 jQuery 后,您可以调用与获取数据和填充元素相关的众多 AJAX 函数之一。有 $.load()、$.ajax() 和其他一些我无法理解的内容,除非我去查看他们的文档部分。

您可以在没有 jQuery 的情况下完成所有这些操作,但它需要更多代码,并且您必须控制浏览器差异。

I think I use document.getElementById('displaywidget') to get the div but how do I then "write/insert/load" displaywidget.php inside the div?

You're looking for the AJAX behaviors inside of jQuery which would make the call to the php page and then push the data into the div.

You should be loading jQuery early on in the process, right up front in your head element. Once its loaded it will be cached so no worries of its on every page. No real overhead incurred.

Once jQuery is installed you can call one of many AJAX functions related to obtaining data and popluation elements. Theres $.load(), $.ajax(), and a few others that escape me unless I go and check out their docs section.

You can do all of this without jQuery, but its more code and you have to control for browser differences.

我ぃ本無心為│何有愛 2024-10-12 00:00:27

您可以将 jquery 加载到 script.js 中,只需将其复制并粘贴到 script.js 中的任何 javascript 之后或之前即可。

因此,如果 script.js 是:

//start of file
alert('ex');
//end of file

则执行:

//start of file
alert('ex')
Copy and pasted Jquery source
//end of file

You can load jquery into script.js, just copy and paste it after or before whatever javascript lives in script.js.

So if script.js is:

//start of file
alert('ex');
//end of file

Make it:

//start of file
alert('ex')
Copy and pasted Jquery source
//end of file
找回味觉 2024-10-12 00:00:27

经过更多的“拖网和思考”后,我找到了这段代码:

(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src","http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script_tag.onload = scriptLoadHandler;
script_tag.onreadystatechange = function () { // Same thing but for IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main(); 
}
/******** Our main function ********/
function main() { 
jQuery(document).ready(function($) { 
******* Load CSS *******/
var css_link = $("<link>", { 
rel: "stylesheet", 
type: "text/css", 
href: "style.css" 
});
css_link.appendTo('head');          

/******* Load HTML *******/
var jsonp_url = "http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=?";
$.getJSON(jsonp_url, function(data) {
$('#example-widget-container').html("This data comes from another server: " + data.html);
});
});
}
})(); // We call our anonymous function immediately

由 Alex Marandon 编写,并在这里找到 http:// alexmarandon.com/articles/web_widget_jquery/ - 非常有用,正是我想要的,包括/将 JQuery 安装到 .js 文件中

After a bit more "trawling & thought" I found this code:

(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src","http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script_tag.onload = scriptLoadHandler;
script_tag.onreadystatechange = function () { // Same thing but for IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main(); 
}
/******** Our main function ********/
function main() { 
jQuery(document).ready(function($) { 
******* Load CSS *******/
var css_link = $("<link>", { 
rel: "stylesheet", 
type: "text/css", 
href: "style.css" 
});
css_link.appendTo('head');          

/******* Load HTML *******/
var jsonp_url = "http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=?";
$.getJSON(jsonp_url, function(data) {
$('#example-widget-container').html("This data comes from another server: " + data.html);
});
});
}
})(); // We call our anonymous function immediately

writtend by Alex Marandon and found here http://alexmarandon.com/articles/web_widget_jquery/ - works a treat, exactly what I wanted, including/installing JQuery into a .js file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文