二、JavaScript部分
弹出对话框函数:alert(), prompt(), confirm()
获得输入焦点函数:focus()
转向使用window.location.href
= ""
引入外部 js 使用<script src=""></script>
获取 name 为 formName 的表单并将该表单数据提交到服务器。但这行代码是不能运行的,有两处错误,一是通过表单名称来获取表单得到时候,应该写成document.forms[‘formName’]
,提交表单事件使用submit
方法,需要加上括号,下面给出简单的示例代码:
<code><span><span>form</span><span>action</span>=<span>"a.php"</span><span>meathod</span>=<span>"post"</span><span>name</span>=<span>"login"</span><span>id</span>=<span>"f1"</span>></span> 用户名:<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"username"</span>/><span>input</span><span>type</span>=<span>"button"</span><span>id</span>=<span>"btn"</span><span>value</span>=<span>"提交"</span>/><span><span>form</span>></span><span>script</span><span>type</span>=<span>"text/javascript"</span>><span><span>document</span>.getElementById(<span>"btn"</span>).><span>function</span> () </span>{ <span>if</span> (<span>document</span>.forms[<span>'login'</span>].username.value != <span>''</span>) { <span>document</span>.forms[<span>'login'</span>].submit(); } } <span><span>script</span>></span></code>
<input id="txt" type="text" value="baidu">
编写代码,当鼠标划过文本框,自动选中文本框中的内容。(百度)<code><span><span>input</span><span>id</span>=<span>"txt"</span><span>type</span>=<span>"text"</span><span>value</span>=<span>"baidu"</span>/></span><span>script</span><span>type</span>=<span>"text/javascript"</span>><span><span>var</span> textBox = <span>document</span>.getElementById(<span>'txt'</span>); textBox.onmouseover = <span><span>function</span>()</span>{ <span>this</span>.select(); } </span><span><span>script</span>></span></code>
主窗口:
<code><span><span>a</span><span>href</span>=<span>""</span><span>id</span>=<span>"a1"</span><span>target</span>=<span>"new"</span>></span>新窗口<span><span>a</span>></span><span>div</span><span>id</span>=<span>"msg"</span>><span><span>div</span>></span><span>script</span><span>type</span>=<span>"text/javascript"</span>><span><span>var</span> a1 = <span>document</span>.getElementById(<span>'a1'</span>); a1.onclick = <span><span>function</span> () </span>{ <span>window</span>.open(<span>'new.html'</span>,<span>'new'</span>,<span>'location=no,toolbar=no'</span>); <span>return</span><span>false</span>; } </span><span><span>script</span>></span></code>
弹出窗口:
<code><span><span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"message"</span><span>id</span>=<span>"m1"</span>/></span><span>br</span> /><span>input</span><span>type</span>=<span>"button"</span><span>value="关闭"</span><span>id</span>=<span>"btn"</span>/><span>br</span> /><span>script</span><span>type</span>=<span>"text/javascript"</span>><span><span>var</span> btn = <span>document</span>.getElementById(<span>'btn'</span>); <span>var</span> message = <span>document</span>.getElementById(<span>'m1'</span>); btn.onclick = <span><span>function</span>()</span>{ <span>var</span> div = <span>window</span>.opener.document.getElementById(<span>'msg'</span>); div.innerHTML = message.value; <span>window</span>.close(); } </span><span><span>script</span>></span></code>
使用 XMLHttpRequest 对象异步请求
<code><span>var</span> arr = <span>new</span><span>Array</span>( <span>new</span><span>Array</span>(<span>1</span>,<span>2</span>,<span>3</span>,<span>4</span>), <span>new</span><span>Array</span>(<span>"abc"</span>, <span>"def"</span>, <span>"xyz"</span>), ); <span>for</span>(i = <span>0</span>; i document.write(arr[<span>0</span>]); }</code>
怀疑这个代码写错了,第二个 new array 后面的逗号是多余的,如果此处没问题,则代码是正确的,输出的结果是 1,2,3,4 1,2,3,4
注:arr 是一个二维数组,该数组有两个元素,第一个元素是数组[1,2,3,4],第二个元素也是一个数组为["abc", "def", "xyz"],for 循环语句执行了两次,但都是输出第一个元素,即数组[1,2,3,4]。
<code><span><span>script</span><span>type</span>=<span>"text/javascript"</span>></span><span><span><span>function</span><span>array_unique</span>(<span>arr</span>)</span>{ <span>var</span> result = arr; <span>for</span> (<span>var</span> i = <span>0</span>; i for</span> (<span>var</span> j = <span>0</span>; i // 如果当前元素与后面某一个元素相等,则移除顶元素<span>if</span> ((i+j+<span>1</span>) 1]) { result.splice(i+j+<span>1</span>,<span>1</span>); } } } <span>return</span> result; } <span>var</span> a = [<span>4</span>,<span>7</span>,<span>8</span>,<span>5</span>,<span>8</span>,<span>6</span>,<span>43</span>,<span>7</span>,<span>0</span>,<span>false</span>,<span>''</span>,{}]; <span>var</span> b = array_unique(a); alert(b);<span>//4,5,8,5,6,43,0,false,[object Object]</span><span><span>script</span>></span></code>
A. var obj = ( );
B. var obj = [ ];
C. var obj = { };
D. var obj = //;
答案:A
A. null instanceof Object
B. null === undefined
C. null == undefined
D. NaN == NaN
答案:C
A. foo.att
B. foo("att")
C. foo["att"]
D. foo{"att"}
E. foo["a"+"t"+"t"]
答案:ACE
(1).直接作为元素的属性,如<img src="hello.jpg" alt="php面试题之二——Javascript(基础部分) javascript:void(0); javlibrary.com href javascript" >
(2).使用 DOM 0 级事件,简单,兼容性好,如 img.onclick
= function(){}
(3).使用 DOM 2 级事件,功能更强大,在非 IE 等标准浏览其中,使用 addEventListener,在IE 浏览器中则使用 attachEvent 来实现。
JavaScript 不支持二维数组定义,可以用 arr[0] = new array()来解决。
<code><span><span>script</span><span>type</span>=<span>"text/javascript"</span>></span><span><span><span>function</span><span>go2b</span>() </span>{ <span>window</span>.location.href = <span>"b.html"</span>; <span>window</span>.close(); } setTimeout(<span>"go2b()"</span>,<span>5000</span>);<span>//5秒后自动执行go2b方法</span></span><span><span>script</span>></span></code>
(1). var
img = new Image();
(2).var
img = document.createElement("image")
(3). img.innerHTML
= "<img src="xxx.jpg" alt="php面试题之二——Javascript(基础部分) javascript:void(0); javlibrary.com href javascript" >"
前进: history.forward();
或者 history.go(1);
后退: history.back
();
或者history.go(-1);
<code><span><span>script</span><span>type</span>=<span>"text/javascript"</span>></span><span><span>var</span> div = <span>document</span>.createElement(<span>"div"</span>); <span>var</span> a = <span>document</span>.createElement(<span>"a"</span>); a.href = <span>"http://www.baidu.com"</span>; <span>var</span> span = <span>document</span>.createElement(<span>"span"</span>); span.innerHTML = <span>"百度"</span>; a.appendChild(span); div.appendChild(a); <span>document</span>.body.appendChild(div); </span><span><span>script</span>></span></code>
结果 HTML:<div><a href="http://www.baidu.com"><span>百度</span></a></div>
<code><span>document</span>.getElementById( <span>'button'</span> ).onclick = <span><span>function</span>()</span>{ <span>window</span>.open (<span>'page.html'</span>); }</code>
其中,button 是按钮的 id,page.html 是要弹出的窗口页面。
JavaScript 中包括 5 种基本数据类型,分别是 Number,String,Boolean,Null 和 Undefined。
以上就介绍了php面试题之二——Javascript(基础部分),包括了javascript方面的内容,希望对PHP教程有兴趣的朋友有所帮助。