html中载入自执行getElementById("xx")得到null
<!DOCTYPE HTML>
<html>
<head>
<title>Scope Chain & Closure Example </title>
<script type="text/javascript">
( function initAnchors(){
var anchor = document.getElementById('anchor1');
alert(anchor); //想了好久才知道为什么总是null
})(); //自执行
</script>
</head>
<body> <h1>Scope Chain & Closure Example</h1> <ul>
<li><a href="#" id="anchor1">Anchor 1</a></li>
<li><a href="#" id="anchor2">Anchor 2</a></li>
<li><a href="#" id="anchor3">Anchor 3</a></li>
</ul> </body> </html>
2.应该改为:
<script type="text/javascript">
function initAnchors(){
var anchor = document.getElementById('anchor1');
alert(anchor);
};
</script>
</head>
<body onload="initAnchors()"> //使用onload事件,以前总是觉得自己不会犯这样的错,直到出错后才会真正记在头脑中。
html中载入自执行getElementById("xx")得到null的更多相关文章
- 代码在ie9中不能正确执行
<!DOCTYPE html> <html> <head lang="zh"> <meta charset="UTF-8&quo ...
- javaScript 载入自执行
1.注册可以直接调用f()中的b(),c(),d() .原因?自己想. <!DOCTYPE html> <html> <head> <meta charset ...
- 关于判断语句中如:while not xx: 或者:if not xx: 的含义及用法解析
关于判断语句中如:while not xx: 或者:if not xx: 的含义及用法解析 name='' while not name: name=raw_input(u'请输入姓名:') prin ...
- Unity中脚本的执行顺序总结(@WhiteTaken)
(Editor)以上是Unity官方文档中的截图,脚本在被挂载到物体上,会启用Editor的方法Reset. (Initialization)当执行脚本开始,初始化的过程中,依次执行的是Awake-& ...
- html5 webDatabase 存储中sql语句执行可嵌套使用
html5 webDatabase 存储中sql语句执行可嵌套使用,代码如下: *); data.transaction(function(tx){ tx.executeSql("creat ...
- js中的延迟执行和定时执行
在js中,延迟执行函数有两种,setTimeout和setInterval,用法如下: function testFunction(){Console.log('hovertree.com');} s ...
- [转]js中confirm实现执行操作前弹出确认框的方法
原文地址:http://www.jb51.net/article/56986.htm 本文实例讲述了js中confirm实现执行操作前弹出确认框的方法.分享给大家供大家参考.具体实现方法如下: 现在在 ...
- Unity3D中脚本的执行顺序和编译顺序
http://www.cnblogs.com/champ/p/execorder.html 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与 ...
- sql server中如何查看执行效率不高的语句
sql server中,如果想知道有哪些语句是执行效率不高的,应该如何查看呢?下面就将为您介绍sql server中如何查看执行效率不高的语句,供您参考. 在测量功能时,先以下命令清除sql se ...
随机推荐
- 使用 Json 文件存储
将爬取到的数据以 Json 文件形式存储: import json import requests req = requests.get('http://www.baidu.com/') data = ...
- MQTT_DEMO
1 /* 2 Copyright (c) 2009-2012 Roger Light <roger@atchoo.org> 3 All rights reserved. 4 5 Redis ...
- openjdk源码阅读导航
转自:http://rednaxelafx.iteye.com/blog/1549577 这是链接帖.主体内容都在各链接中. 怕放草稿箱里过会儿又坑掉了,总之先发出来再说…回头再慢慢补充内容. 先把I ...
- 【LeetCode OJ】Search Insert Position
题目:Given a sorted array and a target value, return the index if the target is found. If not, return ...
- ldap objectclass
LDAP中,一个条目必须包含一个objectClass属性,且需要赋予至少一个值.每一个值将用作一条LDAP条目进行数据存储的模板:模板中包含了一个条目必须被赋值的属性和可选的属性. obj ...
- 例说hg(五)————创建repository
本文由博主原创,转载请注明出处(保留此处和链接): IT人生(http://blog.csdn.net/robinblog/article/details/17933747) 有很多网站提供了免费的M ...
- 【大数据系列】MapReduce示例一年之内的最高气温
一.项目采用maven构建,如下为pom.xml中引入的jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...
- source.android.google && developer.android.google
https://source.android.google.cn/ https://developer.android.google.cn/ https://source.android.com/co ...
- Google APK下载
在线下载google play中apk的网站 1.http://apps.evozi.com/apk-downloader 2.http://downloader-apk.com/ 3.http:// ...
- ant重新编译打包hadoop-core-1.2.1.jar时遇到的错
错误1. [root@MyDB01 hadoop]# ant -Dversion=1.2.1 examples 错误: 找不到或无法加载主类 org.apache.tools.ant.launch.L ...