2

特定のフィールドで 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は正常に動作しますが、実行時にソースを表示すると、UIHintを使用するすべてのフィールドに対してjqueryスクリプトが参照され、複数回呼び出されることがわかります。JavaScriptコードの重複を防ぐメカニズムはありますか?

4

1 に答える 1

1

そのテンプレートを使用するすべてのアイテムに対して実行される、エディター テンプレートに Javascript を配置しないでください。

代わりに、別の JS ファイルに配置し、その JS ファイルをレイアウトで呼び出すようにします。その後、ページの読み込み時に一度実行され、クラスに割り当てたすべてのアイテムがhtmlmarkerエディター テンプレートに含まれます。

于 2013-02-01T10:23:27.233 に答える