JavaScript的DOM操作。Window.document对象

时间:2023-03-08 21:23:45

间隔执行一段代码:window.setlnteval("需要执行的代码",间隔毫秒数)

例 :      window.setlnteval("alert("你好")", 5000)      5秒后执行一次输出"你好"

1、Window.document对象

docunment.getElementById(" id  ")   根据id找,最多找一个

var a= docunment.getElementById(" id  ")   将找到的元素放到变量中(赋值)

docunment.getElementsByName("name")  根据name找名字,找出来的是数组

docunment.getElementsByTagName("name")   根据标签名找,找出来的是数组

docunment.getElementsByClassName("name") 根据classname找,找出来的是数组

2、获取非表单元素

获取内容:

a.innerHTML  将标签中的HTML代码和文字都获取

ainnerText  只获取里面的文字    获取标签内部的值,用这两个属性

格式如下 ;输出的值为<b>1234567</b>

JavaScript的DOM操作。Window.document对象

例:获取文本框中提交的value值

<body>
<form>
请输入文字:<input type="text" value="" id="wz"/> /*文本域*/
<input type="button" value="按钮" onclick="nn()"> /*普通按钮,鼠标点击时*/
</form> </body>
</html>
<script>
function nn() /*函数,需要调用才能执行*/
{
var a= document.getElementById("wz").value /*获取id为wz的标签中的value值*/
alert("您输入的文字为"+a) /*输出*/
} </script>

效果图如下:

  JavaScript的DOM操作。Window.document对象

在a标签中添加 onclick="return flase" 则不会跳转链接

操作内容:

a.setAttribute("属性名","属性值")   设置一个属性进行更改

a.getAttribute("属性名")    获取属性的值

a.removeAttribute("属性名")  移除属性

例:

要求在文本框中输入123,并点击按钮

<form>
请在文本框中输入123:<input type="text" value="" id="wz" sr="123"/> /*文本框中需要输入123*/
<input type="button" value="按钮" onclick="nn()"> /*普通按钮,鼠标点击时执行函数*/
</form> </body>
</html>
<script>
function nn() /*函数*/
{
var a= document.getElementById("wz") /*获取到id为wz的标签,并将获取到的元素赋值给a*/
var a1=a.value /*将a中的value值赋值给a1*/
var b= a.getAttribute("sr"); /*获取a中的sr属性的值,并赋值给b*/
if(a1==b) /*判断,当a1=b(文本框中输入的值与我们设定的值相等时)*/
{
alert("输入正确") /*输出*/
}
} </script>

  

2.延迟按钮 ,在十秒之后可点击

<form>

<input  type="button" id="a" disabled="disabled" value="十秒之后可点击(10)" />    /*设置一个按钮,并且不可点击*/

</form>
</body>
</html>
<script> var sj=10;
var b=document.getElementById("a");
function ty()
{
sj--;
if(sj==0)
{
b.removeAttribute("disabled")
b.value("同意")
}
else
{
a.value="十秒之后可点击("+sj+")"
window.setTimeout("ty()",1000)
}
}

  效果图如下:

JavaScript的DOM操作。Window.document对象