所以我读到内联javascript是坏的(不可缓存),所以我尝试将我的所有脚本放在单独的文件中,但是如果它是基于内容生成的,你如何避免内联javascript,例如在ASP.NET- MVC。
$('#id').showPopUp({
text: @Model.Message //Dynamic value from controller
})
我对内联CSS也有疑问,我知道如果你需要超过1种风格,最好使用类,但如果我只需要一种风格,例如:
.width100 {width: 100%;} //CSS stylesheet
<table class="width100"></table>
VS
<table style="width: 100%;"></table>
哪一个性能更好?
抱歉英语不好。 任何帮助将不胜感激。
更新:
如何避免内联动态生成的js?例如:
@{ if (flag = true) }
<script type="text/javascript">
$('#id').getIn();
</script>
@{ else }
<script type="text/javascript">
$('#id').getOut();
</script>
答案 0 :(得分:2)
您必须使用html-5 custom data attribute:
<div id="SomeDiv" data-message="@Model.Message">
</div>
并在js文件中:
$('#SomeDiv').showPopUp({
text: $("#SomeDiv").data("message")
})