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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable