3

我正在尝试在 js 中加载 QUnit,但 QUnit.js 中的 addevent 函数从未被触发,它只是无法正常工作:

var appendQUnit = document.createElement('script'); 
appendQUnit.src = 'js/utility/qunit/qunit.js';
appendQUnit.type = 'text/javascript'; 
document.getElementsByTagName('head')[0].appendChild(appendQUnit); 
4

3 回答 3

6

您可能还需要调用 QUnit.load() 来初始化 QUnit:

$.getScript('js/utility/qunit/qunit.js', function(){
QUnit.load();
// here you can handle the qunit code
});
于 2011-10-20T06:46:03.160 回答
2

您可以使用 jquery 的getScript,例如:

$.getScript('js/utility/qunit/qunit.js', function() {
    // here you can handle the qunit code
});

因为浏览器将始终以异步模式加载 javascript 文件,您将需要一些回调来放置处理新加载的 js 代码的代码。

于 2010-05-20T10:53:54.920 回答
0

在您的代码中使用以下内容或作为书签:

代码

void(function foo()
  {
  /* get head element */
  var head=document.getElementsByTagName("head")[0];

  /* create script and link elements */
  var qUnitJS = document.createElement("script");
  var qUnitCSS = document.createElement("link");

  /* link rel and type attributes required for lazy loading */
  qUnitCSS.rel="stylesheet";
  qUnitCSS.type="text/css";

  /* define script src attribute to lazy load */
  qUnitJS.src = "http://qunitjs.com/resources/qunit.js";

  /* append script and link elements */
  head.appendChild(qUnitJS);
  head.appendChild(qUnitCSS);

  /* define link href after DOM insertion to lazy load */
  qUnitCSS.href="http://qunitjs.com/resources/qunit.css";

  /* call tests after QUnit loads */
  qUnitJS.onload = function () {};
  }() )

书签

javascript:void(function foo(){var head=document.getElementsByTagName("head")[0]; var qUnitJS = document.createElement("script"); var qUnitCSS = document.createElement("link"); qUnitCSS. rel="stylesheet"; qUnitCSS.type="text/css"; qUnitJS.src = "http://qunitjs.com/resources/qunit.js"; head.appendChild(qUnitJS); head.appendChild(qUnitCSS); qUnitCSS.href="http://qunitjs.com/resources/qunit.css"; qUnitJS.onload = function () {};}() )

在 Firefox 中,设置security.mixed_content.block_active_content为 falseabout:config以将混合的活动内容作为书签运行。

参考

于 2013-12-17T02:43:24.817 回答