0

我认为我有这段代码

<a href="#SampleEditor" onclick="javaScript:ShowSampleEditor()"
                    id="SampleLink">Add Sample</a>
                <div class="editor-field" id="SampleEditor" style="display: none">
                    <div class="editor-label">
                        @Html.LabelFor(model => model.SampleCollectionInstructions)
                    </div>
                    <div class="editor-field">
                        @Html.TextAreaFor(model => model.SampleCollectionInstructions, new { @class = "adminRichText" })
                        @Html.ValidationMessageFor(model => model.SampleCollectionInstructions)
                    </div>
                </div>

它的作用是向用户提供一个链接以打开富文本编辑器并隐藏链接

这是我使用的代码

function ShowSampleEditor() {
        $("#SampleEditor").show();
        $("#SampleLink").hide();
    }

现在我必须为更多的编辑做这件事。由于 Json 不是我真正的东西,我怎样才能为几个编辑器创建一个通用函数来做到这一点?

4

1 回答 1

0

将代码编辑为如下所示

 <a href="#SampleEditor" onclick="javaScript:ShowSampleEditor(this)"
                id="SampleLink">Add Sample</a>
            <div class="editor-field" id="SampleEditor" style="display: none">
                <div class="editor-label">
                    @Html.LabelFor(model => model.SampleCollectionInstructions)
                </div>
                <div class="editor-field">
                    @Html.TextAreaFor(model => model.SampleCollectionInstructions, new { @class = "adminRichText" })
                    @Html.ValidationMessageFor(model => model.SampleCollectionInstructions)
                </div>
            </div>

和 javascript 到

function ShowSampleEditor(link) {
   $(link).next().show(); // Next element after the link is the panel
   //$(".editor-field").show(); // Use this if the element to show does not immediately follow the link
   $(link).hide();
}

参见演示小提琴 - http://jsfiddle.net/27MeG/8/

我已经概括了 javascript 函数以将点击的链接作为参数。然后这个链接被隐藏,它的下一个兄弟被显示——它应该是编辑器。

于 2012-11-20T13:44:56.680 回答