js封装中,用this.变量名.length作为循环条件,实现不了循环

蒲小若 2017-08-25 08:07:06

function Slider(opt){
this.elem = opt.dom;
console.log(this.elem)
this.sliders = this.elem.getElementsByClassName("banner");
// console.log(5 < this.sliders.length);
// this.slidersLen = this.sliders.length;

init();
function init(){
renderDom();
}

function renderDom(){
//渲染页面
if (opt.direction == "horizontal") {
for (var i = 0; i < this.sliders.length; i++) {
this.sliders[i].style.position = "relative";
this.style.top = 0;
this.style.left = 0;
}
}

}

function bindDom(){
//绑定事件
}
}

window.onload = function (){
var banner = document.getElementById("container");
new Slider({dom:banner,direction:"horizontal"});
}





出现的问题,求大神
...全文
261 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

    function Slider(opt) {
        this.elem = opt.dom;
        this.sliders = this.elem.getElementsByClassName("banner");

        this.init();
        this.init = function () {
            renderDom.call(this);
        }

        function renderDom() {
            if (opt.direction == "horizontal") {
                for (var i = 0; i < this.sliders.length; i++) {
                    this.sliders[i].style.position = "relative";
                    this.style.top = 0;
                    this.style.left = 0;
                }
            }

        }

        function bindDom() {
            //绑定事件
        }
    }


Web开发学习资料推荐
extjs开发技巧
easyui datagrid fitColumns:true失效解决办法
天际的海浪 2017-08-25
  • 打赏
  • 举报
回复


function Slider(opt){
	this.elem = opt.dom;
	console.log(this.elem)
	this.sliders = this.elem.getElementsByClassName("banner");
//	console.log(5 < this.sliders.length);
//	this.slidersLen = this.sliders.length;
	var _this = this;

	init();
	function init(){
		renderDom();
	}
	
	function renderDom(){
		//渲染页面
		if (opt.direction == "horizontal") {
			for (var i = 0; i < _this.sliders.length; i++) {
				_this.sliders[i].style.position = "relative";
				_this.sliders[i].style.top = 0;
				_this.sliders[i].style.left = 0;
			}
		}
		
	}
	
	function bindDom(){
		//绑定事件
	}
}

window.onload = function (){
	var banner = document.getElementById("container");
	new Slider({dom:banner,direction:"horizontal"});
}

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧