当前位置:Gxlcms > JavaScript > JavaScript中document对象使用详解_基础知识

JavaScript中document对象使用详解_基础知识

时间:2021-07-01 10:21:17 帮助过:7人阅读

对象属性

代码如下:

document.title //设置文档标题等价于HTML的标签<br /> document.bgColor //设置页面背景色<br /> document.fgColor //设置前景色(文本颜色)<br /> document.linkColor //未点击过的链接颜色<br /> document.alinkColor //激活链接(焦点在此链接上)的颜色<br /> document.vlinkColor //已点击过的链接颜色<br /> document.URL //设置URL属性从而在同一窗口打开另一网页<br /> document.fileCreatedDate //文件建立日期,只读属性<br /> document.fileModifiedDate //文件修改日期,只读属性<br /> document.fileSize //文件大小,只读属性<br /> document.cookie //设置和读出cookie<br /> document.charset //设置字符集 简体中文:gb2312<br /> </div></p> <p>======================================================================<br /> body-主体子对象</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code51821"><br /> document.body //指定文档主体的开始和结束等价于<body></body><br /> document.body.bgColor //设置或获取对象后面的背景颜色<br /> document.body.link //未点击过的链接颜色<br /> document.body.alink //激活链接(焦点在此链接上)的颜色<br /> document.body.vlink //已点击过的链接颜色<br /> document.body.text //文本颜色<br /> document.body.innerText //设置<body>...</body>之间的文本<br /> document.body.innerHTML //设置<body>...</body>之间的HTML代码<br /> document.body.topMargin //页面上边距<br /> document.body.leftMargin //页面左边距<br /> document.body.rightMargin //页面右边距<br /> document.body.bottomMargin //页面下边距<br /> document.body.background //背景图片<br /> document.body.appendChild(oTag) //动态生成一个HTML对象<br /> </div></p> <p>常用对象事件</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code29794"><br /> document.body.onclick="func()" //鼠标指针单击对象是触发<br /> document.body.onmouseover="func()" //鼠标指针移到对象时触发<br /> document.body.onmouseout="func()" //鼠标指针移出对象时触发<br /> </div></p> <p>======================================================================<br /> location-位置子对象</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code69315"><br /> document.location.hash // #号后的部分<br /> document.location.host // 域名+端口号<br /> document.location.hostname // 域名<br /> document.location.href // 完整URL<br /> document.location.pathname // 目录部分<br /> document.location.port // 端口号<br /> document.location.protocol // 网络协议(http:)<br /> document.location.search // ?号后的部分<br /> </div></p> <p>常用对象事件</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code50752"><br /> documeny.location.reload() //刷新网页<br /> document.location.reload(URL) //打开新的网页<br /> document.location.assign(URL) //打开新的网页<br /> document.location.replace(URL) //打开新的网页<br /> </div></p> <p>======================================================================<br /> images集合(页面中的图象)<br /> a)通过集合引用</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code32999"><br /> document.images //对应页面上的<img>标签<br /> document.images.length //对应页面上<img>标签的个数<br /> document.images[0] //第1个<img>标签<br /> document.images //第i-1个<img>标签<br /> </div></p> <p>b)通过name属性直接引用</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code59702"><br /> <img name="oImage"><br /> document.images.oImage //document.images.name属性<br /> </div></p> <p>c)引用图片的src属性</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code29446"><br /> document.images.oImage.src //document.images.name属性.src<br /> </div></p> <p>d)创建一个图象</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code96333"><br /> var oImage<br /> oImage = new Image()<br /> document.images.oImage.src="https://img.gxlcms.com/https://img.gxlcms.com/1.jpg"<br /> </div></p> <p>同时在页面上建立一个<img>标签与之对应就可以显示<br /> 示例代码(动态创建图象):</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code95670"><br /> <html><br /> <img name=oImage><br /> <script language="javascript"><br /> var oImage<br /> oImage = new Image()<br /> document.images.oImage.src="https://img.gxlcms.com/https://img.gxlcms.com/1.jpg"<br /> </ script><br /> </html><br /> <html><br /> <script language="javascript"><br /> oImage=document.caeateElement("IMG")<br /> oImage.src="https://img.gxlcms.com/https://img.gxlcms.com/1.jpg"<br /> document.body.appendChild(oImage)<br /> </ script><br /> </html><br /> </div></p> <p>=====================================================================<br /> forms集合(页面中的表单)<br /> a)通过集合引用</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code71103"><br /> document.forms //对应页面上的<br /> document.Myform.myctrl //document.表单名.控件名<br /> </div></p> <p>----------------------------<br /> c)访问表单的属性</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code97142"><br /> document.forms.name //对应 <br /> <script language="javascript"> <br /> function fun(){ <br /> //遍历checkbox控件的值并判断是否选中 <br /> var length <br /> length=document.forms[0].chk.length <br /> for(i=0;i<length;i++){ <br /> v=document.forms[0].chk.value <br /> b=document.forms[0].chk.checked <br /> if(b) <br /> alert(v=v+"被选中") <br /> else <br /> alert(v=v+"未选中") <br /> } <br /> } <br /> </ script> <br /> ddd <br /> </html> <br /> </div></p> <p>----------------------------<br /> 示例代码(Select):</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code91802"><br /> <html><br /> <!--Select控件相关Script--><br /> <br /> <script language="javascript"><br /> //遍历select控件的option项<br /> var length<br /> length=document.Myform.oSelect.length<br /> for(i=0;i<length;i++)<br /> document.write(document.Myform.oSelect.value)<br /> </ script><br /> <script language="javascript"><br /> //遍历option项并且判断某个option是否被选中<br /> for(i=0;i<document.Myform.oSelect.length;i++){<br /> if(document.Myform.oSelect.selected!=true)<br /> document.write(document.Myform.oSelect.value)<br /> else<br /> document.write("<font color=red>"+document.Myform.oSelect.value+"</font>") <br /> }<br /> </ script><br /> <script language="javascript"><br /> //根据SelectedIndex打印出选中的option<br /> //(0到document.Myform.oSelect.length-1)<br /> i=document.Myform.oSelect.selectedIndex<br /> document.write(document.Myform.oSelect.value)<br /> </ script><br /> <script language="javascript"><br /> //动态增加select控件的option项<br /> var oOption = document.createElement("OPTION");<br /> oOption.text="4";<br /> oOption.value="4";<br /> document.Myform.oSelect.add(oOption);<br /> </ script><br /> <html><br /> </div></p> <p>======================================================================<br /> Div集合(页面中的层)</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code56149"><br /> <Div id="oDiv">Text</Div><br /> document.all.oDiv //引用图层oDiv <br /> document.all.oDiv.style.display="" //图层设置为可视<br /> document.all.oDiv.style.display="none" //图层设置为隐藏<br /> document.getElementId("oDiv") //通过getElementId引用对象<br /> document.getElementId("oDiv").<br /> document.getElementId("oDiv").display="none"<br /> /*document.all表示document中所有对象的集合<br /> 只有ie支持此属性,因此也用来判断浏览器的种类*/<br /> </div></p> <p>图层对象的4个属性</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code89666"><br /> document.getElementById("ID").innerText //动态</script></pre>输出文本<br /> document.getElementById("ID").innerHTML //动态</script></pre>输出HTML<br /> document.getElementById("ID").outerText //同innerText<br /> document.getElementById("ID").outerHTML //同innerHTML<br /> </div></p> <p>----------------------------<br /> 示例代码:</p> <p><div class="codetitle"><span><U></U></span> 代码如下:</div><div class="codebody" id="code62558"><br /> <html><br /> <script language="javascript"><br /> function change(){<br /> document.all.oDiv.style.display="none"<br /> }<br /> </ script><br /> <Div id="oDiv" onclick="change()">Text</Div><br /> </html><br /> <html><br /> <script language="javascript"><br /> function changeText(){<br /> document.getElementById("oDiv").innerText="NewText"<br /> }<br /> </ script><br /> <Div id="oDiv" onmouseover="changeText()">Text</Div><br /> </html><br /> </div> </div> <div class=""> <ul class="m-news-opt fix"> <li class="opt-item"> <a href='/JavaScript-217117.html' target='_blank'><p>< 上一篇</p><p class="ellipsis">ECMAScript6即将带给我们新的数组操作方法前瞻_javascript技巧</p></a> </li> <li class="opt-item ta-r"> <a href='/JavaScript-217119.html' target='_blank'><p>下一篇 ></p><p class="ellipsis">jQuery中removeProp()方法用法实例_jquery</p></a> </li> </ul> </div> </div> </div> <div class="g-title fix"> <h2 class="title-txt">人气教程排行</h2> </div> <div class="m-rank u-dashed mb40"> <ul> <li class="rank-item"> <a href="/JavaScript-60002.html" title='vue2 设置router-view默认路径的实例' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">213次</span> <span class="g-sort-num top">1</span> vue2 设置router-view默认路径的实例 </a> </li> <li class="rank-item"> <a href="/JavaScript-56926.html" title='Vue-路由导航菜单栏的高亮设置方法' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">213次</span> <span class="g-sort-num second">2</span> Vue-路由导航菜单栏的高亮设置方法 </a> </li> <li class="rank-item"> <a href="/JavaScript-56852.html" title='基于Axios 常用的请求方法别名(详解)' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">213次</span> <span class="g-sort-num third">3</span> 基于Axios 常用的请求方法别名(详解) </a> </li> <li class="rank-item"> <a href="/JavaScript-39788.html" title='JavaScript+canvas实现七色板效果实例' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">213次</span> <span class="g-sort-num ">4</span> JavaScript+canvas实现七色板效果实例 </a> </li> <li class="rank-item"> <a href="/JavaScript-281548.html" title='实现vuex的初始化方法' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">212次</span> <span class="g-sort-num ">5</span> 实现vuex的初始化方法 </a> </li> <li class="rank-item"> <a href="/JavaScript-238997.html" title='jQuery:当鼠标快速移动时无法触发mouseleave事件的问题解决' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">212次</span> <span class="g-sort-num ">6</span> jQuery:当鼠标快速移动时无法触发mouseleave事件的问题解决 </a> </li> <li class="rank-item"> <a href="/JavaScript-234597.html" title='Array.slice()与Array.splice()的返回值类型_基础知识' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">212次</span> <span class="g-sort-num ">7</span> Array.slice()与Array.splice()的返回值类型_基础知识 </a> </li> <li class="rank-item"> <a href="/JavaScript-60647.html" title='微信小程序使用swiper组件实现层叠轮播图' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">212次</span> <span class="g-sort-num ">8</span> 微信小程序使用swiper组件实现层叠轮播图 </a> </li> <li class="rank-item"> <a href="/JavaScript-57684.html" title='element-ui 限制日期选择的方法(datepicker)' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">212次</span> <span class="g-sort-num ">9</span> element-ui 限制日期选择的方法(datepicker) </a> </li> <li class="rank-item"> <a href="/JavaScript-57417.html" title='vue-baidu-map 进入页面自动定位的解决方案(推荐)' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">212次</span> <span class="g-sort-num ">10</span> vue-baidu-map 进入页面自动定位的解决方案(推荐) </a> </li> <li class="rank-item"> <a href="/JavaScript-54331.html" title='JS实现元素上下左右移动效果' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">212次</span> <span class="g-sort-num ">11</span> JS实现元素上下左右移动效果 </a> </li> <li class="rank-item"> <a href="/JavaScript-32878.html" title='node.js中的emitter.on方法使用说明' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">212次</span> <span class="g-sort-num ">12</span> node.js中的emitter.on方法使用说明 </a> </li> <li class="rank-item"> <a href="/JavaScript-251986.html" title='在js中如何实现图片左右滑动' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">211次</span> <span class="g-sort-num ">13</span> 在js中如何实现图片左右滑动 </a> </li> <li class="rank-item"> <a href="/JavaScript-65218.html" title='layui button 按钮弹出提示窗口,确定才进行的方法' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">211次</span> <span class="g-sort-num ">14</span> layui button 按钮弹出提示窗口,确定才进行的方法 </a> </li> <li class="rank-item"> <a href="/JavaScript-53968.html" title='深入理解Vue生命周期、手动挂载及挂载子组件' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">211次</span> <span class="g-sort-num ">15</span> 深入理解Vue生命周期、手动挂载及挂载子组件 </a> </li> <li class="rank-item"> <a href="/JavaScript-32108.html" title='JS 使用for循环遍历子节点查找元素' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">211次</span> <span class="g-sort-num ">16</span> JS 使用for循环遍历子节点查找元素 </a> </li> <li class="rank-item"> <a href="/JavaScript-286609.html" title='bootstrap如何设置表单必填' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">210次</span> <span class="g-sort-num ">17</span> bootstrap如何设置表单必填 </a> </li> <li class="rank-item"> <a href="/JavaScript-286358.html" title='bootstrap4兼容哪些浏览器' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">210次</span> <span class="g-sort-num ">18</span> bootstrap4兼容哪些浏览器 </a> </li> <li class="rank-item"> <a href="/JavaScript-247980.html" title='jQuery实现追加数组并去重功能' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">210次</span> <span class="g-sort-num ">19</span> jQuery实现追加数组并去重功能 </a> </li> <li class="rank-item"> <a href="/JavaScript-215560.html" title='jQuery实现的在线答题功能_jquery' class="item-name ellipsis" target="_blank"> <span class="g-art-count fr">210次</span> <span class="g-sort-num ">20</span> jQuery实现的在线答题功能_jquery </a> </li> </ul> </div> </div> </div> <!-- / 教程内容页 --> </div> </div> <!-- 页尾 --> <div class="footer"> 本站所有资源全部来源于网络,若本站发布的内容侵害到您的隐私或者利益,请联系我们删除!</div> <!-- / 页尾 --> <script type="text/javascript" src="/kan/js/read.js"></script> <div style="display:none"> <div class="login-box" id="login-dialog"> <div class="login-top"><a class="current" rel="nofollow" id="login1" onclick="setTab('login',1,2);" >登录</a></div> <div class="login-form" id="nav-signin"> <!-- <div class="login-ico"><a rel="nofollow" class="qq" id="qqlogin" target="_blank" href="/user-center-qqlogin.html"> QQ </a></div> --> <div class="login-box-form" id="con_login_1"> <form id="loginform" action="/user-center-login.html" method="post" onsubmit="return false;"> <p class="int-text"> <input class="email" id="username" name="username" type="text" value="用户名或Email" onfocus="if(this.value=='用户名或Email'){this.value='';}" onblur="if(this.value==''){this.value='用户名或Email';};" ></p> <p class="int-text"> <input class="password1" type="password" id="password" name="password" value="******" onBlur="if(this.value=='') this.value='******';" onFocus="if(this.value=='******') this.value='';" > </p> <p class="int-info"> <label class="ui-label"> </label> <label for="agreement" class="ui-label-checkbox"> <input type="checkbox" value="" name="cookietime" id="cookietime" checked="checked" value="2592000"> <input type="hidden" name="notforward" id="notforward" value="1"> <input type="hidden" name="dosubmit" id="dosubmit" value="1">记住我的登录 </label> <a rel="nofollow" class="aright" href="/user-center-forgetpwd.html" target="_blank"> 忘记密码? </a></p> <p class="int-btn"><a rel="nofollow" id="loginbt" class="loginbtn"><span>登录</span></a></p> </form> </div> <form id="regform" action="/user-center-reg.html" method="post"> <div class="login-reg" style="display: none;" id="con_login_2"> <input type="hidden" name="t" id="t"/> <p class="int-text"> <input id="email" name="email" type="text" value="Email" onfocus="if(this.value=='Email'){this.value='';}" onblur="if(this.value==''){this.value='Email';};"></p> <p class="int-text"> <input id="uname" name="username" type="text" value="用户名或昵称" onfocus="if(this.value=='用户名或昵称'){this.value='';}" onblur="if(this.value==''){this.value='用户名或昵称';};"></p> <p class="int-text"> <input type="password" id="pwd" name="password" value="******" onBlur="if(this.value=='') this.value='******';" onFocus="if(this.value=='******') this.value='';"> </p> <p class="int-text1"><span class="inputbox"> <input id="validate" name="validate" type="text" value="验证码" onfocus="if(this.value=='验证码'){this.value='';}" onblur="if(this.value==''){this.value='验证码';};"> </span><span class="yzm-img"><img src="/user-checkcode-index" alt="看不清楚换一张" id="indexlogin"></p> <p class="int-info"> <label> <input value="" name="agreement" id="agreement" CHECKED="checked" type="checkbox"> 我已阅读<a rel="nofollow" href="/user-center-agreement.html">用户协议</a>及<a rel="nofollow" href="/user-center-agreement.html">版权声明</a></label> </p> <p class="int-btn"><input type="hidden" name="dosubmit"/> <a rel="nofollow" class="loginbtn" id="register"><span>注册</span></a></p> </div> </form> </div> </div> </div> </div> <script type="text/javascript" src="/kan/js/foot_js.js"></script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?6dc1c3c5281cf70f49bc0bc860ec24f2"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script type="text/javascript" src="/layui/layui.js"></script> <script> layui.use('code', function() { layui.code({ elem: 'pre', //默认值为.layui-code about: false, skin: 'notepad', title: 'php怎么实现数据库验证跳转代码块', encode: true //是否转义html标签。默认不开启 }); }); </script> </body> </html>