Home  >  Article  >  Backend Development  >  PHP interview question 2 - Javascript (basic part) javascript:void(0); javlibrary.com href javascript

PHP interview question 2 - Javascript (basic part) javascript:void(0); javlibrary.com href javascript

WBOY
WBOYOriginal
2016-07-29 08:53:112614browse

二、JavaScript部分

1. JS 表单弹出对话框函数是?获得输入焦点函数是?

弹出对话框函数:alert(), prompt(), confirm()
获得输入焦点函数:focus()

2. JS 的转向函数是?怎么引入一个外部 JS 文件?

转向使用window.location.href = ""
引入外部 js 使用<script src=""></script>

3. 解释下面语句的意思:document.form["formName"].submit。(百度)

获取 name 为 formName 的表单并将该表单数据提交到服务器。但这行代码是不能运行的,有两处错误,一是通过表单名称来获取表单得到时候,应该写成document.forms[‘formName’],提交表单事件使用submit 方法,需要加上括号,下面给出简单的示例代码:

<code><span><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><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"username"</span>/></span><span><<span>input</span><span>type</span>=<span>"button"</span><span>id</span>=<span>"btn"</span><span>value</span>=<span>"提交"</span>/></span><span></<span>form</span>></span><span><<span>script</span><span>type</span>=<span>"text/javascript"</span>></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></<span>script</span>></span></code>
4. 有下面语句:<input id="txt" type="text" value="baidu" />编写代码,当鼠标划过文本框,自动选中文本框中的内容。(百度)
<code><span><span><<span>input</span><span>id</span>=<span>"txt"</span><span>type</span>=<span>"text"</span><span>value</span>=<span>"baidu"</span>/></span><span><<span>script</span><span>type</span>=<span>"text/javascript"</span>></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></span></code>
5. 设计一个网页,使得打开它时弹出一个全屏的窗口,该窗口中有一个文本框和一个按钮。用户在文本框中输入信息后点击按钮就可以把窗口关闭,而输入的信息却在主网页中显示。(新浪)

主窗口:

<code><span><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><<span>div</span><span>id</span>=<span>"msg"</span>></span><span></<span>div</span>></span><span><<span>script</span><span>type</span>=<span>"text/javascript"</span>></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></span></code>

弹出窗口:

<code><span><span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"message"</span><span>id</span>=<span>"m1"</span>/></span><span><<span>br</span> /></span><span><<span>input</span><span>type</span>=<span>"button"</span><span>value="关闭"</span><span>id</span>=<span>"btn"</span>/></span><span><<span>br</span> /></span><span><<span>script</span><span>type</span>=<span>"text/javascript"</span>></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></span></code>
6. 可以使用哪些方法使用 JavaScript 向服务器发出请求且不离开当前页面,简单对比各自的特点(如果存在)(酷讯)

使用 XMLHttpRequest 对象异步请求

7. 判断以下代码是否正确,如果有错,请指出错误,如果正确,请指出运行结果 (酷讯)
<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 < arr.length; i++) {
    <span>document</span>.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]。

[E]8. 用 JavaScript 写一个函数,其功能是可以删除数组中重复的元素。
<code><span><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 < arr.length; i++) {
        <span>for</span> (<span>var</span> j = <span>0</span>; i < arr.length; j++) {
                temp = arr[i];
                <span>// 如果当前元素与后面某一个元素相等,则移除顶元素</span><span>if</span> ((i+j+<span>1</span>) < arr.length && temp === arr[i+j+<span>1</span>]) {
                    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></<span>script</span>></span></span></code>
9. 以下哪条 JavaScript 语句会产生运行错误:( )

A. var obj = ( );
B. var obj = [ ];
C. var obj = { };
D. var obj = //;
答案:A

10. 请选择结果为真的表达式:( )

A. null instanceof Object
B. null === undefined
C. null == undefined
D. NaN == NaN
答案:C

11. foo 对象有 att 属性,那么获取 att 属性的值,以下哪些做法是可以的:( )

A. foo.att
B. foo("att")
C. foo["att"]
D. foo{"att"}
E. foo["a"+"t"+"t"]
答案:ACE

12. 如何添加 html 元素的事件,有几种方法,举例

(1).直接作为元素的属性,如<img 'hello' );" src="hello.jpg">
(2).使用 DOM 0 级事件,简单,兼容性好,如 img.onclick = function(){}
(3).使用 DOM 2 级事件,功能更强大,在非 IE 等标准浏览其中,使用 addEventListener,在IE 浏览器中则使用 attachEvent 来实现。

13. JavaScript 能否定义二维数组,如果不能你如何解决?

JavaScript 不支持二维数组定义,可以用 arr[0] = new array()来解决。

14. 假设 a.html 和 b.html 在同一个文件夹下面,用 JavaScript 实现当打开 a.html 五秒钟后,自动跳转到 b.html。
<code><span><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></span></code>
15. 请使用 JavaScript 写出三种产生一个 image 标签的方法(提示:从方法、对象、HTML角度考虑)

(1). var img = new Image();
(2).var img = document.createElement("image")
(3). img.innerHTML = "<img src='xxx.jpg' />"

16. js 中网页前进和后退的代码

前进: history.forward();或者 history.go(1);
后退: history.back ();或者history.go(-1);

17. 请写出一条至少 3 个节点的 DOM 树 (YG)
<code><span><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></span></code>

结果 HTML:
<div><a href="http://www.baidu.com"><span>百度</span></a></div>

18. 实现点击按钮弹出窗口的代码 (YG)
<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 是要弹出的窗口页面。

19. JavaScript 包括那些基本数据类型?(小米)

JavaScript 中包括 5 种基本数据类型,分别是 Number,String,Boolean,Null 和 Undefined。

以上就介绍了php面试题之二——Javascript(基础部分),包括了javascript方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn