本文實例講述了ASP.NET MVC中使用JavaScriptResult的用法。分享給大家供大家參考,具體如下:
在頁面中我們使用JavaScript來設置id為qubernet的span標簽樣式,具體代碼如下:
JS:
1
2
3
4
5
6
|
<script> $( function () { var thisObj = document.getElementById( "qubernet" ); thisObj.setAttribute( "style" , "color:#f00;font-size:22px;" ); }); </script> |
Html:
1
2
3
|
< body > < span id = "qubernet" >qubernet span!!!</ span > </ body > |
運行效果:
以上的效果我們也可以使用MVC中的JavaScriptResult來實現,具體如下:
控制器方法:
1
2
3
4
5
6
|
[HttpPost] public JavaScriptResult GetJavaScriptResult() { const string thisJs = "var thisObj = document.getElementById(\"qubernet\");thisObj.setAttribute(\"style\",\"color:#f00;font-size:22px;\");" ; return JavaScript(thisJs); } |
JS:
1
2
3
4
5
6
7
8
|
<script> $( function () { $.ajax({ type: "post" , url: "/QuberManage/SysLogin/GetJavaScriptResult" }); }); </script> |
Html:
1
2
3
|
< body > < span id = "qubernet" >qubernet span!!!</ span > </ body > |
注:這里使用到了jQuery的ajax方法,當然需要引入jQuery庫了,你懂得
希望本文所述對大家asp.net程序設計有所幫助。