<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style>
#showCountry {
position: absolute;display: none;
width: 200px;height: 100px;
border: 1px solid red;
background-color: blue;color: white;
}
</style>
<script>
var list = {
'zg': ['中国', '北京', '牡丹', '世界第二大经济体'],
'mg': ['美国', '华盛顿', '玫瑰', '白人与黑人一起劳动,却想到仇视'],
'rb': ['日本', '东京', '樱花', '世界文明的两样东西:忍者和A片'],
'hg': ['韩国', '首尔', '无穷', '民族意识超强']
};
onload = function () {
//获取所有图片
var imgs = document.getElementsByTagName('img');
//为每个图片指定指向、移开事件
for (var i = 0; i < imgs.length; i++) {
imgs[i].onmouseover = function(e) {//指向国旗显示国家信息
//获取国家信息
var msg = list[this.id];
//构造描述字符串
var msgStr = '国家:' + msg[0] + '<br>首都:' + msg[1] + '<br>国花:' + msg[2] + '<br>描述:' + msg[3];
//获取div
var showCountry = document.getElementById('showCountry');
//显示div
showCountry.style.display = 'block';
//设置描述信息
showCountry.innerHTML = msgStr;
//让div在鼠标的位置显示
showCountry.style.left = e.clientX + 'px';
showCountry.style.top = e.clientY + 'px';
};
imgs[i].onmouseout = function() {//移开国旗隐藏显示
//获取div
var showCountry = document.getElementById('showCountry');
showCountry.style.display = 'none';
};
}
};
</script>
</head>
<body>
<img id="zg" title="abc" src="images/zg.jpg" width="100" height="100" />
<img id="mg" src="images/mg.jpg" width="100" height="100" />
<img id="rb" src="images/rb.jpg" width="100" height="100" />
<img id="hg" src="images/hg.jpg" width="100" height="100" />
<div id="showCountry"></div>
</body>
</html>