【问题标题】:jQuery insert html after comment tagjQuery在评论标签后插入html
【发布时间】:2016-03-18 20:30:53
【问题描述】:

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

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

【问题讨论】:

标签: jquery html append comments insertafter


【解决方案1】:

使用来自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)

【讨论】:

    猜你喜欢
    • 2011-11-29
    • 2017-09-27
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多