mvc javascript multiple times

mvc javascript multiple times

本文关键字:times multiple javascript mvc      更新时间:2023-09-26

如果要求某些字段使用 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代码的重复吗?

不要把你的 Javascript 放在编辑器模板中,编辑器模板将针对使用该模板的每个项目执行

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