dongyi6183 2013-12-12 00:10
浏览 51
已采纳

如何防止css加载查询被应用到整个页面?

I am loading content from an external php file and inserting it into a using query's $.get functionality. The included file contains various css classes. For example, to style hyperlinks. When the file is loaded and the content inserted by jquery, the css styles are not only applied to the new content but also the entire page. To elaborate, I'm loading the content of an html email. Inside the email content is css to style hyperlinks within the email message. The message content is retrieved from the server using a php script and echoed. jQuery gets the content returned from the php script and puts it in the div's inner html with:

$.get("file.php",function(data){ $("#div").html(data); });

But the css in the email content (returned by file.php) gets applied to the entire page, replacing styles on my own hyperlinks. How can I prevent the new css from being applied to the entire page, and have it only apply to the newly loaded content within the div container without changing the rest of the page's original css?

  • 写回答

2条回答 默认 最新

  • dongqian2021 2013-12-12 00:20
    关注

    2 possible solutions.

    1) Try using an iframe and just populate its content with the CSS and HTML.

    2) Maybe try and add a <div> wrapper that has an ID then just prepend the ID to all of the definitions in the CSS?

    EX:

    <div id="emailWrapper">
     ... Email Content/CSS ...
    </div>
    

    CSS (Before):

    .someClass {
      .. attributes ..
    }
    

    CSS (After):

    #emailWrapper .someClass {
      .. attributes ..
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制