jQuery的点击加载脚本 [英] jquery load script on click

查看:91
本文介绍了jQuery的点击加载脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码从另一个站点加载JavaScript脚本,但是它不起作用!

I'm attempting to load a javascript script from another site with this code but it isnt working!

    $("#random").click(function(){
        $.getScript("http://www.themoviequotes.com/widgets/javascript?n=1&l=1&g=3");
    });

我有一个ID为电影"的div,我正在其中尝试从上述脚本中将内容加载到其中!

I have a div with an id of "movie" in which i'm trying to load the content into from the above script!

推荐答案

text/html模式下,<script>元素本质上是CDATA.因此&amp;表示&amp;而不是&.

In text/html mode, <script> elements are intrinsically CDATA. Thus &amp; means &amp; and not &.

请勿在<script>元素内使用HTML实体.

Do not use HTML entities inside <script> elements.

(如果您正在编写XHTML并将其用作text/html,则指南有关XHTML媒体类型的信息,请特别注意嵌入式样式表中的部分和脚本.)

(If you are writing XHTML and serving it as text/html, then the guidance on XHTML media types with special attention for the section on embedded style sheets and scripts.)

由于您已将&替换为&amp,因此您正在从服务器无法识别的URL提取数据.

Since you have replaced & with &amp, you are fetching data from a URL that the server doesn't recognize.

但是,即使您确实进行了更改,它仍然无法正常工作.该脚本(出于某种原因被用作text/html而不是application/javascript)包含一个document.write语句.如果文档仍处于打开状态以进行追加,则此操作将写入文档的末尾(因此对于替换页面的一部分无用),否则将其完全替换.

However, even if you did change that, it still wouldn't work. The script, which for some reason is being served as text/html instead of application/javascript, contains a document.write statement. This writes to the end of the document (so it is useless for replacing a section of the page) if the document is still open for appending, or replaces it entirely otherwise.

jQuery.load将无济于事,因为那样会导致某些内容掉入一个元素(而不是要执行的脚本)并且(据我所知)没有跨域功能(仅适用于非常新的浏览器和仅适用于发送正确HTTP标头的服务器.

jQuery.load wouldn't help since that expects some content to drop into an element (not a script to execute) and (as far as I know) has no cross-domain capabilities (which only work in very new browsers and only with servers which send the right HTTP headers).

这篇关于jQuery的点击加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆