简体   繁体   English

使用 jquery 注入 HTML 和 CSS 但有 Z2C56C360580420D293172FB2 单独文件?

[英]Inject HTML and CSS using jquery but have the CSS in a separate file?

I'm building something that is going to inject HTML into a users website via Jquery hosted on my server (their just going to include my script like they do other Javascript already).我正在构建一些东西,它将通过托管在我的服务器上的 Jquery 将 HTML 注入用户网站(他们只是会像其他 Javascript 一样包含我的脚本)。

So I have my HTML injecting;所以我有我的 HTML 注入; how can I not write inline style CSS with the injected HTML?如何不使用注入的 HTML 编写内联样式 CSS?

The only idea I have is to query my server for this external javascript file at the beginning of my Jquery.我唯一的想法是在我的 Jquery 的开头向我的服务器查询这个外部 javascript 文件。 I'm not sure how to make both the CSS and Jquery available to the user's browser though.我不确定如何使 CSS 和 Jquery 都可用于用户的浏览器。

Any thoughts?有什么想法吗? I'm doing something like the below, FYI.我正在做类似下面的事情,仅供参考。

$(document).ready(function() {
$('body').find(":last").after("
<div class='spacer'></div><div id='nav_menu_wrapper'><div class='nav_menu'>");
});

You can use jQuery to inject a <link> tag with the href pointing to wherever you serve the script:您可以使用 jQuery 注入<link>标记,其href指向您提供脚本的任何位置:

$('head').append(
    $('<link/>', {href: '//path.to/your/stylesheet.css', rel: 'stylesheet'})
);

You can append a CSS on head .你可以把 append 和 CSS 放在head

$("<link />").attr({ "href": "link",
    "rel": "stylesheet",
    "type": "text/css" })
    .appendTo("head");

Or make a request and create a style tag.或者提出请求并创建style标签。

$.get("/static/default.css", function(d)
{
    $("<style />").html(d).appendTo("head");
})

As for appending html you can make a $.get and append or use an iframe .至于附加html您可以制作$.get和 append 或使用iframe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM