简体   繁体   English

如何避免内联JavaScript与生成的内容和CSS内联性能比较

[英]How to avoid inline javascript with generated content and CSS inline performance comparison

So I read that inline javascript is bad (not cacheable), so I try to put all my script in separate file, but how do you avoid inline javascript if it is generated based on content, for example in ASP.NET-MVC. 因此,我读到内联javascript不好(不可缓存),因此我尝试将所有脚本放在单独的文件中,但是如果基于内容(例如在ASP.NET-MVC中)生成内联javascript,如何避免使用内联javascript。

$('#id').showPopUp({
    text: @Model.Message //Dynamic value from controller
})

I also have question about inline CSS, I understand that it is best to use class if you need more than 1 style, but what if I only need one style, for example : 我也有关于内联CSS的问题,我知道如果需要多个样式,最好使用class,但是如果我只需要一种样式,该怎么办,例如:

.width100 {width: 100%;} //CSS stylesheet
<table class="width100"></table>

VS VS

<table style="width: 100%;"></table>

which one is better in performance? 哪一个性能更好?

Sorry for bad english. 对不起,英语不好。 Any help will be appreciated. 任何帮助将不胜感激。

UPDATE: 更新:

And how do I avoid inline for dynamic generated js? 以及如何避免动态生成js的内联? For example : 例如 :

@{ if (flag = true) }
<script type="text/javascript">
    $('#id').getIn();
</script>

@{ else }
<script type="text/javascript">
    $('#id').getOut();
</script>

You have to use html-5 custom data attribute for this: 您必须为此使用html-5自定义数据属性

<div id="SomeDiv" data-message="@Model.Message">
</div>

and in js file: 并在js文件中:

$('#SomeDiv').showPopUp({
    text: $("#SomeDiv").data("message")
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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