【问题标题】:mvc javascript multiple timesmvc javascript多次
【发布时间】:2013-02-01 10:20:50
【问题描述】:

如果有某些字段使用 htmleditor 的要求。因此,对于使用 htmleditor 的每个字段,我创建了一个编辑器模板,该模板使用 jquery 呈现 htmleditor,为此我使用 UIHint。

@model System.String

<script type="text/javascript" src="~/Scripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" language="javascript">
    tinyMCE.init({
        mode: "textareas",
        theme: "advanced",
        editor_selector: "htmlmarker",
        theme_advanced_buttons1: "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,         justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,    |,bullist,numlist,|,outdent,indent,     blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,    |,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3: "hr,removeformat,visualaid,|,sub,sup",

        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left"

    });

</script>
@Html.TextAreaFor(model => model, new {@class="htmlmarker" })

现在 evreything 工作正常,但在执行时,当我查看源代码时,我发现 jquery 脚本被引用并为使用 UIHint 的每个字段多次调用。有没有什么机制可以防止 javascript 代码的重复?

【问题讨论】:

    标签: c# javascript asp.net-mvc razor


    【解决方案1】:

    不要将您的 Javascript 放在编辑器模板中,这将执行每个使用该模板的项目

    相反,将它放在一个单独的 JS 文件中,在您的布局上调用该 JS 文件,然后在页面加载时,它将执行一次并包含您分配给类 @987654321 的 所有 项@ 在您的编辑器模板中。

    【讨论】:

      猜你喜欢
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      相关资源
      最近更新 更多