IE6的javascript问题
<!--[if IE 6]>
<script language="JavaScript">
function correctPNG()
{
for(var i=0; i<document.images.length; i++)
{
var img = document.images[i]
var imgName = img.src.toUpperCase()
if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
{
var imgID = (img.id) ? "id='" + img.id + "' " : ""
var imgClass = (img.className) ? "class='" + img.className + "' " : ""
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
var imgStyle = "display:inline-block;" + img.style.cssText
if (img.align == "left") imgStyle = "float:left;" + imgStyle
if (img.align == "right") imgStyle = "float:right;" + imgStyle
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
var strNewHTML = "<span "+ imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src='" + img.src + "', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML
i = i-1
}
}
}
if (window.attachEvent) window.attachEvent("onload", correctPNG);
</script>
<![endif]-->
谁能帮我翻译一下这段代码?这段代码是做什么的?感激不尽
追问:这段代码主要做了个什么事?
答案:<!--[if IE 6]>
<script language="JavaScript">
function correctPNG()
{
for(var i=0; i<document.images.length; i++)//循环遍历当前页面的所图片
{
var img = document.images[i]//根据循环,获取当前循环的图片对象
var imgName = img.src.toUpperCase() //获取图片对象推向的src,并且地址变成大写(图片的src地址 如:/IMGES/AA.PNG)
if (imgName.substring(imgName.length-3, imgName.length) == "PNG")//截取如片对象的src,获取图片对象的后缀名,判断是否是PNG格式的图片
{
var imgID = (img.id) ? "id='" + img.id + "' " : "" //看图片是否有id,有就获取,没有就为空
var imgClass = (img.className) ? "class='" + img.className + "' " : "" //看图片是否有class,有就获取,没有就为空
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' " //看图片是否有title,有就获取,没有就为空
var imgStyle = "display:inline-block;" + img.style.cssText //定义css字符串变量,( img.style.cssText 获取图片对象的原有的 css)
if (img.align == "left") imgStyle = "float:left;" + imgStyle
if (img.align == "right") imgStyle = "float:right;" + imgStyle
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle//判断图片推向父元素是否为a 标签,如果是,则加个 手型(cursor:hand)的css元素
var strNewHTML = "<span "+ imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src='" + img.src + "', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML //(outerHTML:的解释 http://baike.zhaoxi.net/image/b29f82829fc389ac6c81198b)
i = i-1//(这个我还着没看出来作用,我觉得有这个是不对的)
}
}
}
if (window.attachEvent) window.attachEvent("onload", correctPNG);// if (window.attachEvent) : 表示是ie浏览器,这个是给ie动态注册一个onlond事件
</script>
<![endif]-->
遍历所有的图片,如果图片引用地址为“PNG”结尾的话,强制重新定义它的CSS样式,
包括:
css的class;
style:display:inline-block;如果是左对齐:float:left;
如果是右对齐:float:right;
摸上去的时候鼠标改成hand(cursor:hand)
上一个:Javascript 改变 onclick的动作
下一个:javascript 正则表达式的问题