在 .js 页面中包含 Jquery 脚本
抱歉,如果标题不太清楚,但我认为这是正确的。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
但是如何在 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.
您可以将 jquery 加载到 script.js 中,只需将其复制并粘贴到 script.js 中的任何 javascript 之后或之前即可。
因此,如果 script.js 是:
则执行:
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:
Make it:
经过更多的“拖网和思考”后,我找到了这段代码:
由 Alex Marandon 编写,并在这里找到 http:// alexmarandon.com/articles/web_widget_jquery/ - 非常有用,正是我想要的,包括/将 JQuery 安装到 .js 文件中
After a bit more "trawling & thought" I found this code:
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