我有这个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>我找不到enlive助手函数来删除标记。
我已经找到了解决方案,谢谢你的例子。所以我写了这段代码,js就消失了
(html/deftemplate template-about "../resources/public/build/about/index.html"
[]
[:script] (fn js-clear [& args] nil)
)发布于 2013-10-24 16:41:02
当我需要有条件地删除呈现页面的一些标记时,我通常的方法是使用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。
发布于 2013-10-24 17:53:31
如果您不介意进行额外的解析和发送,那么下面的操作会很好:
(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))发布于 2013-10-24 20:07:37
删除整个标记时,只需使用nil作为转换。
(deftemplate tester1
(java.io.StringReader. "<html><body><div class=\"content\">...")
[]
[:script] nil)
(template1)在您的示例中,您将用正在使用的资源替换StringReader。
https://stackoverflow.com/questions/19571639
复制相似问题