Skip to content

Instantly share code, notes, and snippets.

@prinsss
Last active March 1, 2018 06:07
Show Gist options
  • Save prinsss/a12f8fe71c2e3d7a2c0fcedbc6d625d2 to your computer and use it in GitHub Desktop.
Save prinsss/a12f8fe71c2e3d7a2c0fcedbc6d625d2 to your computer and use it in GitHub Desktop.
$('.load-gist').on('click', function() {
var gist = {
// URL 传参自己改下
url: '//gist.github.com/printempw/' + $(this).data('hash') + '.json?callback=?',
dom: this
}
// 因为 URL 中带了 `callback=?`,所以这里 jQuery 会使用 JSONP 方式请求
$.getJSON(gist.url, function(data) {
// 加载样式
gist.style = document.createElement('link');
gist.style.type = 'text/css';
gist.style.rel = 'stylesheet';
gist.style.href = data.stylesheet;
document.getElementsByTagName('HEAD')[0].appendChild(gist.style);
// 移除加载按钮
$(gist.dom).after(data.div).remove();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment