搜索
javascript - JS获取元素对属性赋值失败
巴扎黑
巴扎黑 2017-04-10 16:25:08
[JavaScript讨论组]

在演示地址中,我对id为describe的子元素图片的尺寸进行统一赋值width="655",昨日都是正确的,今天调试过程中却失败,我的js代码地址为

window.onload = function(){
                    var prev = document.getElementById('prev');
                    var next = document.getElementById('next');
                    var sImg = document.getElementById('slider-img1');
                    var sCation = document.getElementById('slider-cation');
                    var arrayImg = ['http://dag1688.aliapp.com/wp-content/uploads/2015/11/dag-1.jpg','http://dag1688.aliapp.com/wp-content/uploads/2015/11/dag-2.jpg','http://dag1688.aliapp.com/wp-content/uploads/2015/11/dag-3.jpg','http://dag1688.aliapp.com/wp-content/uploads/2015/11/dag-4.jpg'];
                    var arrayText = ['大光五金旗舰店实拍','国际国内一线品牌代理商','30年五金机电行业服务经验','贵州省各大工矿企业优秀供应商'];
                    var num = 0;
                    
                    document.getElementById('pIcon').onmouseover = function () {
                        document.getElementById('pIcon').style.color = "green";
};
                    document.getElementById('pIcon').onmouseout = function () {
                        document.getElementById('pIcon').style.color = "gray";
};
                    document.getElementById('nIcon').onmouseover = function () {
                        document.getElementById('nIcon').style.color = "green";
};
                    document.getElementById('nIcon').onmouseout = function () {
                        document.getElementById('nIcon').style.color = "gray";
};
                    sImg.onmouseover = function(){
                        sImg.style.filter = "brightness(100%)";
                    };
                    
                    sImg.onmouseout = function(){
                        sImg.style.filter = "brightness(200%)";
                    };
                    //幻灯片初始化 

                function checkout (){
                            sImg.src = arrayImg[num];
                            sCation.innerHTML = arrayText[num];
                };
                    checkout();

                    prev.onclick = function(){
                          num --;
                            if(num==-1){
                                num = 3;
                            }
                            checkout();
                    };
                    next.onclick = function(){
                          num ++;
                            if(num == 4){
                                num = 0;
                                    }
                        checkout();
                    };
                    
// 以下是宽度为655的图片样本格式化
var pImg = document.getElementsByClassName('describe')[0];
var img = pImg.getElementsByTagName('img');
var htmlinline = document.getElementsByClassName('htmlinline')[0].innerHTML;
for(var nu =2;nu<img.length-5;nu++){
img[nu].alt = htmlinline;
img[nu].width = "655";
}
};

最后7行为演示错误的代码,请各位大神指教

巴扎黑
巴扎黑

全部回复(1)
迷茫

你的测试地址中,

var pImg = document.getElementsByClassName('describe')[0];

获取到的DOM对象是:

<p class="describe">
      东西不错,比超市的要便宜。
</p>

木有图片啊...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号