prototype.js学习笔记 客官°小女子只卖身不卖艺 2022-08-25 05:25 141阅读 0赞 1. **Prototype.js定义:** Prototype.js是一个javascript类库。该类库包含了web开发所需要的大部分常用方法。开发者可以直接调用无需写新的function()。 1. **实用的函数:** * $()方法 $()方法是DOM中document.getElementById()的简写。可以传入多个id作为参数然后$()返回一个带有所有要求的元素的Arrary对象。 详看[http://www.w3school.com.cn/htmldom/met\_doc\_getelementbyid.asp][http_www.w3school.com.cn_htmldom_met_doc_getelementbyid.asp]。 举例: <table style="color:rgb(51,51,51)"> <tbody> <tr> <td> <p><span style="color:teal"><!</span><span style="color:rgb(63,127,127)">DOCTYPE </span><span style="color:navy">HTML </span><span style="color:gray">PUBLIC </span><span style="color:navy">"-//W3C//DTD HTML 4.01 Transitional//EN"</span><span style="color:rgb(63,127,95)">"http://www.w3.org/TR/html4/loose.dtd"</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">html</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">head</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">title</span><span style="color:teal">></span><span style="color:black">Test Prototype.js</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">title</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">meta </span><span style="color:rgb(127,0,127)">http-equiv</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"Content-Type" </span><span style="color:rgb(127,0,127)">content</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/html; charset=UTF-8"</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript" </span><span style="color:rgb(127,0,127)">src</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"JS/prototype_3.js"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript" </span><span style="color:rgb(127,0,127)">src</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"JS/validation_1.js"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript" </span><span style="color:rgb(127,0,127)">src</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"JS/effects_2.js"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></p> <p><span style="color:teal"></</span><span style="color:rgb(63,127,127)">head</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">body</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript"</span><span style="color:teal">></span></p> <p><span style="color:black"> </span><span style="color:rgb(127,0,85)">function </span><span style="color:black">test1()</span></p> <p><span style="color:black"> </span><span style="color:rgb(127,0,85)">{ </span></p> <p><span style="color:rgb(127,0,85)"> var </span><span style="color:black">d = $(</span><span style="color:rgb(142,0,255)">'myDiv'</span><span style="color:black">);</span></p> <p><span style="color:black"> alert(d.innerHTML);</span></p> <p><span style="color:black"> </span><span style="color:rgb(127,0,85)">}</span></p> <p><span style="color:teal"></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">div </span><span style="color:rgb(127,0,127)">id</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"myDiv"</span><span style="color:teal">></span></p> <p><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">p</span><span style="color:teal">></span><span style="color:black">This is a paragraph</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">p</span><span style="color:teal">></span></p> <p><span style="color:teal"></</span><span style="color:rgb(63,127,127)">div</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">input </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"button" </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">Test1 </span><span style="color:rgb(127,0,127)">onclick</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"test1();"</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">br</span><span style="color:teal">></span></p> <p><span style="color:teal"></</span><span style="color:rgb(63,127,127)">body</span><span style="color:teal">></span></p> <p><span style="color:teal"></</span><span style="color:rgb(63,127,127)">html</span><span style="color:teal">></span></p> </td> </tr> </tbody> </table> 运行结果: * $F()方法 $F()方法能够返回任何表单(form)输入控件的值(value)。该方法可以用元素id或元素本身作为参数。 其实现原理:document.formname.module.value 举例: <table style="color:rgb(51,51,51)"> <tbody> <tr> <td> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript"</span><span style="color:teal">></span></p> <p><span style="color:rgb(127,0,85)">function </span><span style="color:black">test3()</span></p> <p><span style="color:rgb(127,0,85)">{ </span></p> <p><span style="color:black"> </span><span style="color:black">alert( $F(</span><span style="color:rgb(142,0,255)">'userName'</span><span style="color:black">) );</span></p> <p><span style="color:rgb(127,0,85)">}</span></p> <p><span style="color:teal"></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></p> <p> </p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">input </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text" </span><span style="color:rgb(127,0,127)">id</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"userName" </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"Joe Doe"</span><span style="color:teal">><</span><span style="color:rgb(63,127,127)">br</span><span style="color:teal">></span></p> <p><span style="color:teal"><</span><span style="color:rgb(63,127,127)">input </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"button" </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">Test3 </span><span style="color:rgb(127,0,127)">onclick</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"test3();"</span><span style="color:teal">><</span><span style="color:rgb(63,127,127)">br</span><span style="color:teal">></span></p> <p> </p> </td> </tr> </tbody> </table> 运行结果: * $A()函数 $A()函数能把接收到的单个参数转换成Arrary对象。 * $H()函数 $H()函数把一些对象转换成一个可枚举的联合数组类似的Hash对象。 * $R()函数 $R()是new ObjectRange(lowBound,upperBound,excludeBounds)的缩写。 * Try.these()函数 Try.these()方法把一系列的方法作为参数并且按顺序的一个个执行这些方法,直到其中一个成功执行,返回成功执行的方法的返回值。 1. **Ajax对象** Ajax对象是一个预定义对象,由这个包创建,为了封装和简化编写Ajax功能涉及的狡猾的代码。这个对象包含一系列的封装Ajax逻辑的类。 · Ajax.Request class继承自Ajax.Base class,它创建XMLHttpRequest对象简单且支持多种类型的浏览器。 **Method:\[ctor\](url, options)** 举例:用Ajax.Request对象和服务器通信 <table style="color:rgb(51,51,51)"> <tbody> <tr> <td> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">html</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">head</span><span style="color:teal">><</span><span style="color:rgb(63,127,127)">title</span><span style="color:teal">></span><span style="color:black">Test Prototype.js</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">title</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">meta </span><span style="color:rgb(127,0,127)">http-equiv</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"Content-Type" </span><span style="color:rgb(127,0,127)">content</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/html; charset=UTF-8"</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript" </span><span style="color:rgb(127,0,127)">src</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"JS/prototype_3.js"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript" </span><span style="color:rgb(127,0,127)">src</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"JS/validation_1.js"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript" </span><span style="color:rgb(127,0,127)">src</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"JS/effects_2.js"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">head</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">body</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript"</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">function </span><span style="color:black">searchSales()</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">{ </span></span></p> <p><span style="font-size:10px"><span style="color:rgb(127,0,85)"> var </span><span style="color:black">empID = $F(</span><span style="color:rgb(142,0,255)">'lstEmployees'</span><span style="color:black">);</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">var </span><span style="color:black">y = $F(</span><span style="color:rgb(142,0,255)">'lstYears'</span><span style="color:black">);</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">var </span><span style="color:black">url = </span><span style="color:rgb(142,0,255)">'http://localhost/mytest/test.php'</span><span style="color:black">;</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">var </span><span style="color:black">pars = </span><span style="color:rgb(142,0,255)">'empID=' </span><span style="color:black">+ empID + </span><span style="color:rgb(142,0,255)">'&year=' </span><span style="color:black">+ y;</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(63,95,191)">//alert(pars);</span></span></p> <p><span style="font-size:10px"><span style="color:rgb(63,95,191)"> </span><span style="color:rgb(127,0,85)">var </span><span style="color:black">myAjax = </span><span style="color:rgb(127,0,85)">new</span><span style="color:black"> Ajax.Request(</span></span></p> <p><span style="font-size:10px"><span style="color:black"> url,</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">{ </span></span></p> <p><span style="font-size:10px"><span style="color:rgb(127,0,85)"> </span><span style="color:black">method: </span><span style="color:rgb(142,0,255)">'get'</span><span style="color:black">,</span></span></p> <p><span style="font-size:10px"><span style="color:black"> parameters: pars,</span></span></p> <p><span style="font-size:10px"><span style="color:black"> onComplete: showResponse</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">}</span><span style="color:black">); </span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">}</span></span></p> <p><span style="font-size:10px"><span style="color:rgb(127,0,85)"> function </span><span style="color:black">showResponse(originalRequest)</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">{ </span></span></p> <p><span style="font-size:10px"><span style="color:rgb(127,0,85)"> </span><span style="color:rgb(63,95,191)">//put returned XML in the textarea</span></span></p> <p><span style="font-size:10px"><span style="color:rgb(63,95,191)"> </span><span style="color:black">$(</span><span style="color:rgb(142,0,255)">'result'</span><span style="color:black">).value = originalRequest.responseText;</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">}</span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">div</span><span style="color:teal">><</span><span style="color:rgb(63,127,127)">select </span><span style="color:rgb(127,0,127)">id</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"lstEmployees" </span><span style="color:rgb(127,0,127)">size</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"10" </span><span style="color:rgb(127,0,127)">onchange</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"searchSales()"</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">option </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"5"</span><span style="color:teal">></span><span style="color:black">Buchanan, Steven</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">option</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">option </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"8"</span><span style="color:teal">></span><span style="color:black">Callahan, Laura</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">option</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">option </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"1"</span><span style="color:teal">></span><span style="color:black">Davolio, Nancy</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">option</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">select</span><span style="color:teal">></span><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">select </span><span style="color:rgb(127,0,127)">id</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"lstYears" </span><span style="color:rgb(127,0,127)">size</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"3" </span><span style="color:rgb(127,0,127)">onchange</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"searchSales()"</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">option </span><span style="color:rgb(127,0,127)">selected</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"selected" </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"1996"</span><span style="color:teal">></span><span style="color:black">1996</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">option</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">option </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"1997"</span><span style="color:teal">></span><span style="color:black">1997</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">option</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">option </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"1998"</span><span style="color:teal">></span><span style="color:black">1998</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">option</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">select</span><span style="color:teal">></span><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">br</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">textarea </span><span style="color:rgb(127,0,127)">id</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">result </span><span style="color:rgb(127,0,127)">cols</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">60 </span><span style="color:rgb(127,0,127)">rows</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">10</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">textarea</span><span style="color:teal">></span><span style="color:black"> </span><span style="color:teal"><</span><span style="color:rgb(63,127,127)">br</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">div</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">body</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">html</span><span style="color:teal">></span></span></p> </td> </tr> </tbody> </table> · Ajax.Updater class **Method:\[ctor\](container, url, options) ** Container一般为容器的id 1. **prototype.js 参考** prototype.js中包含了许多常用类和方法,使用时可以参照pdf手册。以下列举的是prototype中最基本的部分类和函数可以主要学习。 · JavaScript 类的扩展 · The Ajax.Request class · The Ajax.Updater class · The Element.ClassNames class · The Form object · Position 对象 函数用法举例: <table style="color:rgb(51,51,51)"> <tbody> <tr> <td> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">html</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">head</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">title</span><span style="color:teal">></span><span style="color:black">Test Prototype.js</span><span style="color:teal"></</span><span style="color:rgb(63,127,127)">title</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">meta </span><span style="color:rgb(127,0,127)">http-equiv</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"Content-Type" </span><span style="color:rgb(127,0,127)">content</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/html; charset=UTF-8"</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript" </span><span style="color:rgb(127,0,127)">src</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"JS/prototype_3.js"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript" </span><span style="color:rgb(127,0,127)">src</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"JS/validation_1.js"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript" </span><span style="color:rgb(127,0,127)">src</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"JS/effects_2.js"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">head</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">body</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">script </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"text/javascript"</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">function </span><span style="color:black">testStripTags() </span><span style="color:rgb(127,0,85)">{ </span><span style="color:rgb(127,0,85)">//</span><span style="color:rgb(127,0,85)">去除字符串中的</span><span style="color:rgb(127,0,85)">html</span><span style="color:rgb(127,0,85)">标签</span></span></p> <p><span style="font-size:10px"><span style="color:rgb(127,0,85)"> var </span><span style="color:black">htmlString = </span><span style="color:rgb(142,0,255)">"<textarea>okokokokokokokokokok</textarea>"</span><span style="color:black">;</span></span></p> <p><span style="font-size:10px"><span style="color:black"> alert(htmlString.stripTags());</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">} </span></span></p> <p><span style="font-size:10px"><span style="color:rgb(127,0,85)"> function </span><span style="color:black">testEscapeHTML() </span><span style="color:rgb(127,0,85)">{ </span><span style="color:rgb(127,0,85)">//</span><span style="color:rgb(127,0,85)">合理转义字符串中的</span><span style="color:rgb(127,0,85)">html</span><span style="color:rgb(127,0,85)">标签</span></span></p> <p><span style="font-size:10px"><span style="color:rgb(127,0,85)"> var </span><span style="color:black">htmlString = </span><span style="color:rgb(142,0,255)">"<textarea>okokokokokokokokokok</textarea>"</span><span style="color:black">;</span></span></p> <p><span style="font-size:10px"><span style="color:black"> alert(htmlString.escapeHTML());</span></span></p> <p><span style="font-size:10px"><span style="color:black"> </span><span style="color:rgb(127,0,85)">}</span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">script</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">div</span><span style="color:teal">><</span><span style="color:rgb(63,127,127)">input </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">button </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"test stripString"</span><span style="color:rgb(127,0,127)">onclick</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"testStripTags();"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">div</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"><</span><span style="color:rgb(63,127,127)">div</span><span style="color:teal">><</span><span style="color:rgb(63,127,127)">input </span><span style="color:rgb(127,0,127)">type</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">button </span><span style="color:rgb(127,0,127)">value</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"test EscapeHTML"</span><span style="color:rgb(127,0,127)">onclick</span><span style="color:black">=</span><span style="color:rgb(42,0,255)">"testEscapeHTML();"</span><span style="color:teal">></</span><span style="color:rgb(63,127,127)">div</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">body</span><span style="color:teal">></span></span></p> <p><span style="font-size:10px"><span style="color:teal"></</span><span style="color:rgb(63,127,127)">html</span><span style="color:teal">></span></span></p> </td> </tr> </tbody> </table> 运行结果: stripTags()结果: escapeHTML()结果: [http_www.w3school.com.cn_htmldom_met_doc_getelementbyid.asp]: http://www.w3school.com.cn/htmldom/met_doc_getelementbyid.asp
相关 「学习笔记」学习笔记合集 可以点击 [https://www.cnblogs.com/hongzy/tag/%E7%AC%94%E8%AE%B0/][https_www.cnblogs.com_hong 淩亂°似流年/ 2023年06月05日 12:48/ 0 赞/ 20 阅读
相关 学习笔记 学习笔记 sudo adduser lilei sudo usermod -G sudo lilei sudo deluse 客官°小女子只卖身不卖艺/ 2022年11月26日 12:58/ 0 赞/ 27 阅读
相关 学习笔记 \ajax: 1、概念:异步的JavaScript 和 xml 1.1异步和同步:客户端和服务器端相互通信的基础上 \客户端必须等待服务器端的响应。在等待的期间客户 深藏阁楼爱情的钟/ 2022年10月29日 13:24/ 0 赞/ 281 阅读
相关 学习笔记 一. CSS 如何实现文字的垂直居中 1. 二.问题记录 1.创建新的JSP页面的时候报错:The superclass “javax.servlet.http.H 超、凢脫俗/ 2022年08月20日 09:30/ 0 赞/ 144 阅读
相关 【学习笔记】git学习笔记 使用git的好处 可以保存每个版本,只要在每个版本做完后进行上传 ![这里写图片描述][70] 可以异地读取更新 爱被打了一巴掌/ 2022年05月14日 09:10/ 0 赞/ 407 阅读
相关 学习笔记 我的第一天学习c\ 1、c\学习网址 [https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide 矫情吗;*/ 2022年05月08日 06:16/ 0 赞/ 326 阅读
相关 学习笔记 测试 ORM JPA EJB JPQL MOM JMS ORM 对象关系映射 英语:Object Relational M 爱被打了一巴掌/ 2022年02月16日 01:57/ 0 赞/ 397 阅读
相关 [笔记] Docker 学习笔记 1. 什么是 Docker > 官方文档:[链接][Link 1],中文文档:[链接][Link 2] Docker 属于 Linux 容器的一种封装,提供简单易用的容 缺乏、安全感/ 2021年11月27日 02:01/ 0 赞/ 591 阅读
相关 学习笔记 1、js如何将136分钟转化为几小时,几分钟 return (Math.floor(minutes/60) + "小时" + (minutes%60) + "分" 爱被打了一巴掌/ 2021年07月25日 23:46/ 0 赞/ 1051 阅读
还没有评论,来说两句吧...