我有这个HTML代码段。我希望解析该片段并发出没有javascript标签的HTML
<html>
<body>
<div class="content">lorem ipsum</div>
<script src="/js/jquery.js" type="text/javascript"></script>
<script src="/js/bootstrap.min.js" type="text/javascript"></script>
</body>
</html>
成为这个
<html>
<body>
<div class="content">lorem ipsum</div>
</body>
</html>
我无法找到活跃的帮助函数来删除标记。
我已经找到了解决方案,感谢您的示例。所以我写了这段代码,js消失了
(html/deftemplate template-about "../resources/public/build/about/index.html"
[]
[:script] (fn js-clear [& args] nil)
)
答案 0 :(得分:2)
当我需要有条件地删除呈现页面的某些标签时,我通常使用nil returning
函数。
例如
(html/defsnippet upgrade-plan "page_templates/upgrade-plan.html" [:#upgradePlanSection]
[pending-invoice ... ]
...
[:#delayedPlanWarning] #(when pending-invoice
(html/at %
[:#delayedPlanWarning] (html/remove-attr :style)
[:.messagesText] (html/html-content (tower/t :plans/pending-invoice
(:id pending-invoice)))))
...
在特定情况下,如果pending-invoice
为nil
,则从呈现的html中删除delayedPlanWarning
元素,因为该函数返回nil
。
答案 1 :(得分:0)
如果你不介意额外的解析和发射,下面的内容会很好:
(def orig (html-resource (java.io.StringReader. "<html>
<body>
<div class=\"content\">lorem ipsum</div>
<script src=\"/js/jquery.js\" type=\"text/javascript\"></script>
<script src=\"/js/bootstrap.min.js\" type=\"text/javascript\"></script>
</body>
</html>")))
(def stripped (transform orig [(keyword "script")] (substitute "")))
(apply str (emit* stripped))
答案 2 :(得分:0)
删除整个代码时,您只需使用nil
作为转换。
(deftemplate tester1
(java.io.StringReader. "<html><body><div class=\"content\">...")
[]
[:script] nil)
(template1)
在您的情况下,您将StringReader
替换为您正在使用的资源。