如何在开发中使用tailwind和iFrame?
目前,当我添加新的样式时,我会创建一个新的CSS文件:
npx tailwindcss build src/assets/css/tailwind.css -o dist/assets/css/index.css
在开发过程中,我想将该文件链接到我的iFrame中,但这有点麻烦...有更好的方法吗?
如何在开发中使用tailwind和iFrame?
目前,当我添加新的样式时,我会创建一个新的CSS文件:
npx tailwindcss build src/assets/css/tailwind.css -o dist/assets/css/index.css
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<script>
function etw() {
var iframe = document.getElementById("iframe");
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
var tw = iframeDoc.createElement("script");
tw.setAttribute("src", "https://cdn.tailwindcss.com");
tw.onload = function () {
iframeDoc.body.innerHTML = iframeDoc.body.innerHTML; //re-render
};
iframeDoc.head.appendChild(tw);
}
</script>
<body>
<iframe id="iframe" src="/hello.html" title="IFrame"></iframe>
<button onclick="etw()">click to enable tailwind in the iframe</button>
</body>
</html>
hello.html:
<div class="bg-red-500 text-xl p-5 rounded-full">
class="bg-red-500 text-xl p-5 rounded-full" div
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<script>
function etw() {
var iframe = document.getElementById("iframe");
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
var tw = iframeDoc.createElement("script");
tw.setAttribute("src", "https://cdn.tailwindcss.com");
tw.onload = function () {
iframeDoc.body.innerHTML = iframeDoc.body.innerHTML; //re render
};
iframeDoc.head.appendChild(tw);
}
window.addEventListener("DOMContentLoaded", function () {
iframe.contentWindow.addEventListener("DOMContentLoaded", etw);
});
</script>
<body>
<iframe id="iframe" src="/hello.html" title="IFrame"></iframe>
</body>
</html>
使用Play CDN可以在浏览器中尝试Tailwind,无需任何构建步骤。Play CDN仅用于开发目的,不是生产环境的最佳选择。