2015-12-13 3 views
3

Как вставить html после <!-- example comment --> в главный документ. Например, я хочу добавить css в раздел главы с jquery после определенного тега комментария.jQuery insert html после комментария тег

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

http://stackoverflow.com/questions/1623734/selecting-html-comments-with-jquery – mplungjan

ответ

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)

Смежные вопросы