这节学习如何替换网页的javascript语句。。。。
直接上代码:
Private Sub Button6_Click(sender As Object,e As EventArgs) Handles Button6.Click Dim strScript As String = "" strScript &= "function check()" strScript &= "{" strScript &= " if (sampleform.studentname.value.length <2)" strScript &= " {" strScript &= " alert(""姓名长度不符合"");" strScript &= " return false;" strScript &= " }" strScript &= " if (sampleform.studentage.value.length <2)" strScript &= " {" strScript &= " alert(""年龄太小"");" strScript &= " return false; " strScript &= " }" strScript &= " sampleform.submit();" strScript &= "}" Dim win As mshtml.HTMLWindow2 win = wbMain.Document.Window.DomWindow win.execScript(strScript,"javascript") End Sub
原来网页上的check()代码:
function check() { if(sampleform.studentname.value=="") { alert("姓名没有填写"); return false; } if(sampleform.studentage.value.length>2) { alert("年龄长度超过2位"); return false; } sampleform.submit(); }
function check() { if (sampleform.studentname.value.length <2) { alert("姓名长度不符合"); return false; } if (sampleform.studentage.value.length <2) { alert("年龄太小"); return false; } sampleform.submit(); }
看看效果:
使用前:
使用后:
如果原来的网页中没有check()呢?自然会创建一个。
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录