使外部CSS文件仅适用于某些HTML

| 我正在开发一个网站更新程序。我的想法是让某些HTML元素成为可更新类的一部分。然后,我将将该类的每个元素更改为textarea元素。这样就可以对其进行更新。 我将updater.html页面分为2个区域;左侧是select元素,但右侧显示了我正在更新的网页中的一些HTML。我希望右侧的所有html都受特定的CSS样式表(我正在更新的网页的样式表)影响。我怎样才能做到这一点? 因为样式表是外部的,所以我不想打开样式表,将其读入内存(在JavaScript中是可能的),然后将其插入包含可更新HTML的iframe中。
<html>
<head>

</head>
<body>
   <table>
      <tr>
        <td class=\"updaterSection\">
          <select>
            <option> home </option>
             ...
          </select>
        </td>

        <td class=\"updatableWebpage\">
            <!-- I want all the HTML in this div to be affected by the external css stlyle sheet -->
            <iframe src=\"/css/specStyle.css\">
              <p> uneditable stuff </p>
              <textarea> editable stuff </textarea>
            </iframe>
        </td>
       </tr>
     </table>
</body>
</html>
    
已邀请:
据我所知,这不是您使用iframe的方式, ѭ1的内部将替换为src中放置的页面。 您发布的代码仅用于在网页上显示CSS页面。 (一个使用您的代码的简单示例:http://jsfiddle.net/W5ggT/) 因此,您可以将iframe中的所有html放到一个新的html页面中,该页面的标题中包含指向特殊样式表的链接。 或者,您可以使用一些Ajax刷新它,但是我不确定是否完全了解您想要的内容。     
您可以使用javascript在iframe中操纵html网页的DOM。 只有在同一域中才有可能。
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">
<head>
    <title>Iframe manipulation</title>
    <script type=\'text/javascript\'>
        window.onload = function () {
    var iframe = document.getElementById(\'iframe\');
    // cross browser solution for handling iframe
    var contentProp = (iframe.contentWindow)?iframe.contentWindow.document:iframe.contentDocument;

    var head = contentProp.getElementsByTagName(\'head\')[0];
    var link = document.createElement(\'link\');
    link.rel = \"stylesheet\";
    link.type = \"text/css\";
    link.href = \"/stylesheet/css.css\"; // the new stylesheet to add to iframe
    head.appendChild(link);// adds stylesheet to iframe

        });
    </script>
</head>
<body>
<iframe id=\'iframe\' src=\'test2.html\'></iframe>
</body>
</html>
    
在标题部分(带有标记)中添加external.css您的CSS文件。
 <html>
    <head>
          <link rel=\"stylesheet\" type=\"text/css\" href=\"path to the css file\">
    </head>

 <body> </body>

</html>



give a different class name to the tag that you want to style. But the style in your external.css     file.

Hope it helps.
    

要回复问题请先登录注册