jQuery 在评论标签后插入 html

jQuery insert html after comment tag

如何在head文档的<!-- example comment -->之后插入html。例如,我想在头部部分附加 css 并在特定注释标记后添加 jquery。

$('<link>', {
    rel: 'stylesheet',
     href: url
 }).appendTo($("<!-- Related css to this page -->"));

使用 selecting html comments with jquery

中的代码
var url ="bla.css";
$(function() {
  $("head").contents().filter(function() {
    return this.nodeType == 8;
  }).each(function(i, e) {
    if ($.trim(e.nodeValue) == "Related css to this page") {
      $('<link>', {
        rel: 'stylesheet',
        href: url
      }).insertAfter(e);
      return false; // stop immediately - remove if f.ex. url is an array of css
    }
  });
});

FIDDLE (which is using body since head is inserted by JSFiddle)