0

Simile を使用して動的なタイムラインを描画しています。また、社内ライブラリを使用してコメント ブログを追加しています。社内ライブラリは body 要素の onload イベントを使用して初期化します。

<body onload="initComments('myID')">

しかし、simile は initComments('myID') が決して実行されないように、独自の目的のために onload をハイジャックしたようです。

Simile コードを変更する以外に、イニシャライザを実行するにはどうすればよいですか?

問題を解決するためだけに、別のライブラリ (jQuery など) を追加したくありません。

4

6 に答える 6

1

前述のjQueryソリューション(ただし、使用することを強くお勧めしますが、素晴らしいです)とは別に、プレーンなバニラJSソリューションを次に示します(質問でjQueryについて何も言及していないため):

// Add this to top of your script.
window.onload = function () {
    for (var i = 0; arguments.callee.functions.length > i; i++) {
        arguments.callee.functions[i]();
    }
};
window.onload.functions = [];

// Now for every onload function you want to add, do:
window.onload.functions.push(initComments('myID'));
于 2009-11-09T21:39:44.457 に答える
1

jQueryに追加して使用する

$(document).ready(function(){
  // Your code here...
});

これを jQuery で記述する方法は他にもいくつかありますが、これが最もわかりやすいと思います。

なんらかの JavaScript 開発を行っている場合は、jQuery に目を向ける必要があります。それは完全に甘いです!

于 2009-11-09T21:39:54.630 に答える
0

ここを見ると、jQueryが使用するソリューション、またはそれを変更したソリューションが表示されると思いますが、ニーズに合わせて機能するはずです。

http://dean.edwards.name/weblog/2006/06/again/

// Dean Edwards/Matthias Miller/John Resig

function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) clearInterval(_timer);

  // do stuff
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
    if (this.readyState == "complete") {
      init(); // call the onload handler
    }
  };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      init(); // call the onload handler
    }
  }, 10);
}

/* for other browsers */
/*window.onload = init; */

最後の行がコメントアウトされていることがわかります。そこまで進んだ場合は、それらを上書きする (コメントを外す) か、コードが実行されないかのいずれかになりますが、これは主要なブラウザーでは機能します。

于 2009-11-09T22:03:27.717 に答える