JavaScript入门

来源:互联网 发布:ios 检测网络好不好 编辑:程序博客网 时间:2024/06/09 00:11

一 、基本内容

(1)var txt=document.getElementById("p1")

(2)函数function funcname(){

   Content of a function

   }

(3)输入框<input type="button" value="show" onclick="link">

二、常用互动方法

(1)输出<script>document.write(“文本内容"+var+"<br>");

(2)警告alert(var)

(3)确认    confirm

var mymessage=confirm("你的性别");

    if(mymessage==true)

    {

   document.write("你是女士!");

    }

    else

    {

        document.write("你是男士!");

    }

(4)提问prompt

score =prompt("请输入成绩",100);

if(score>=90)

{

   document.write("你很棒!");

}

    else

{

       document.write("要努力了!");

}

(5)打开新窗口 window.open([URL],[窗口名称]0,[参考字符串])

[URL]网页网址或者路径

[窗口名称]

_top:覆盖原网页

_blank新窗口显示

_self当前窗口显示

[参考字符串]

top:窗口离上边距离

left:窗口离左边距离

width

height

(6)关闭窗口 窗口对象.close

var mywin=window.open("http://www.baidu.com");
    mywin.close();

三、DOM操作

(1)innerHTML属性

object.innerHTML用于获取或者替换HTML元素的内容。(object为元素对象,如通过getElementById获取的元素)

(2)改变HTML样式object.style.property

property:backgroundColor;height;width;color;font(在一行设置所有的字体属性);fontFamily(字体系列);fontSize(字体大小)

(3)显示和隐藏object.style.display=value(隐藏none 显示block)

(4)控制类名(className属性)

object.className="NewclassName"

原创粉丝点击