事件上的JQuery加载脚本(单击)

Ric*_*nop 3 javascript jquery

$('#selector').click(function() {
    // here I would like to load a javascript file
    // let's say /js/script-on-click-event.js
});
Run Code Online (Sandbox Code Playgroud)

这有可能吗?我不确定,但我记得在JQuery文档中读到了一个函数,但我在这里找不到它,也许它已被弃用或者我在其他地方看到了它?

基本上我想要的是在点击事件上加载脚本.

Cle*_*ton 8

你可以只使用javascript做同样的事情:

var script = document.createElement('script');
script.src = 'http://somesite.com/somescript.js';
script.type = 'text/javascript';
document.body.parentNode.appendChild(script);
Run Code Online (Sandbox Code Playgroud)


vsy*_*ync 7

jQuery.getScript这样的 东西

$("#selector").click(function(){
  $.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js");
});
Run Code Online (Sandbox Code Playgroud)