首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >enlive删除html标记

enlive删除html标记
EN

Stack Overflow用户
提问于 2013-10-24 16:35:17
回答 3查看 1K关注 0票数 1

我有这个html片段。我希望解析这个片段并发出没有javascript标记的html。

代码语言:javascript
复制
<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>

变成这样

代码语言:javascript
复制
<html>
    <body>
        <div class="content">lorem ipsum</div>
    </body>
</html>

我找不到enlive助手函数来删除标记。

我已经找到了解决方案,谢谢你的例子。所以我写了这段代码,js就消失了

代码语言:javascript
复制
(html/deftemplate template-about "../resources/public/build/about/index.html"
    []
    [:script] (fn js-clear [& args] nil)
)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-24 16:41:02

当我需要有条件地删除呈现页面的一些标记时,我通常的方法是使用nil returning函数。

例如

代码语言:javascript
复制
(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-invoicenil,则从呈现的html中删除delayedPlanWarning元素,因为函数返回nil

票数 2
EN

Stack Overflow用户

发布于 2013-10-24 17:53:31

如果您不介意进行额外的解析和发送,那么下面的操作会很好:

代码语言:javascript
复制
(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))
票数 0
EN

Stack Overflow用户

发布于 2013-10-24 20:07:37

删除整个标记时,只需使用nil作为转换。

代码语言:javascript
复制
(deftemplate tester1
             (java.io.StringReader. "<html><body><div class=\"content\">...")
             []
             [:script] nil)

(template1)

在您的示例中,您将用正在使用的资源替换StringReader

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19571639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档