简体   繁体   中英

Injecting html via javascript/jquery?

I have some mvc3 code that I'm taking ownership and there are views that both apply html helpers and html javascript injection within views.

Personally, I prefer raw html or helpers as opposed to html injection. What are the pro's and cons of each?

Basic html and helpers :

        <div id="efI" class="ef">
            @Html.PasswordFor(m => m.Password)
            @Html.ValidationMessageFor(m => m.Password)
        </div>
        <div>
           @Html.ActionLink("Click here.", "SomeAction")
        </div>

Injection: :

    @Html.EditorForModel()  //this generates controls from above...
    <script type="text/javascript">
       $('#efI').append('<div>@Html.ActionLink("Click here.", "SomeAction")</div>');
    </script>

Not really much difference in the static example you provide. Usually injections are made with dynamically created HTML (for example,table cells managed by AJAX calls to a DB).

It depends. Injection in my opinion is more expensive, it makes some javascript calls in your case. Injection maybe suitable when using templates of html (jquery.tmpl).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM