关于js标签的一些疑问

shexm123 2016-09-21 05:39:17
代码: function b(){
btn.addEventListener('click',a,false);
}
function a(){
var btn=document.getElementById("btn");
btn.value=parseInt(btn.value)+1
}
b();
当把function a(){}改为a:function(){}就不能用了。求大神帮我解答。
...全文
182 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
SkyGst 2016-09-22
  • 打赏
  • 举报
回复
{a:function(){},b:function(){}} 这是函数的结构,用的是json方式。也可以称之为对象字面量。
shexm123 2016-09-21
  • 打赏
  • 举报
回复
我是看了一个轮播代码产生的困惑 function ZoomPic () { this.initialize.apply(this, arguments) } ZoomPic.prototype = { initialize : function (id) { var _this = this; this.wrap = typeof id === "string" ? document.getElementById(id) : id; this.oUl = this.wrap.getElementsByTagName("ul")[0]; this.aLi = this.wrap.getElementsByTagName("li"); this.prev = this.wrap.getElementsByTagName("span")[0]; this.next = this.wrap.getElementsByTagName("span")[1]; this.timer = 1000; this.aSort = []; this.iCenter = 2; this._doPrev = function () {return _this.doPrev()}; this._doNext = function () {return _this.doNext()}; this.options = [ {width:365, height:252, top:40, left:0, zIndex:1}, {width:405, height:280, top:20, left:60, zIndex:2}, {width:445, height:308, top:0, left:130, zIndex:3}, {width:405, height:280, top:20, left:240, zIndex:2}, {width:366, height:252, top:40, left:345, zIndex:1}, ]; for (var i = 0; i < this.aLi.length; i++) this.aSort[i] = this.aLi[i]; this.aSort.unshift(this.aSort.pop()); this.setUp(); this.addEvent(this.prev, "click", this._doPrev); this.addEvent(this.next, "click", this._doNext); this.doImgClick(); this.timer = setInterval(function () { _this.doNext() }, 3000); this.wrap.onmouseover = function () { clearInterval(_this.timer) }; this.wrap.onmouseout = function () { _this.timer = setInterval(function () { _this.doNext() }, 3000); } }, doPrev : function () { this.aSort.unshift(this.aSort.pop()); this.setUp() }, doNext : function () { this.aSort.push(this.aSort.shift()); this.setUp() }, doImgClick : function () { var _this = this; for (var i = 0; i < this.aSort.length; i++) { this.aSort[i].onclick = function () { if (this.index > _this.iCenter) { for (var i = 0; i < this.index - _this.iCenter; i++) _this.aSort.push(_this.aSort.shift()); _this.setUp() } else if(this.index < _this.iCenter) { for (var i = 0; i < _this.iCenter - this.index; i++) _this.aSort.unshift(_this.aSort.pop()); _this.setUp() } } } }, setUp : function () { var _this = this; var i = 0; for (i = 0; i < this.aSort.length; i++) { this.aSort[i].index = i; if (i < 5) { this.css(this.aSort[i], "display", "block"); this.doMove(this.aSort[i], this.options[i] )} } }, addEvent : function (oElement, sEventType, fnHandler) { return oElement.addEventListener ? oElement.addEventListener(sEventType, fnHandler, false) : oElement.attachEvent("on" + sEventType, fnHandler) }, css : function (oElement, attr, value) { if (arguments.length == 2) { return oElement.currentStyle ? oElement.currentStyle[attr] : getComputedStyle(oElement, null)[attr] }//oElement.currentStyle全局方法获取样式,IE浏览器获取外//部样式的实现方式为oElement.currentStyle属性。 else if (arguments.length == 3)//定义样式 { switch (attr) { case "width": case "height": case "top": case "left": case "bottom": oElement.style[attr] = value + "px"; break; case "opacity" : oElement.style.filter = "alpha(opacity=" + value + ")"; oElement.style.opacity = value / 100; break; default : oElement.style[attr] = value; break } } }, doMove : function (oElement, oAttr, fnCallBack) { var _this = this; clearInterval(oElement.timer); oElement.timer = setInterval(function () { var bStop = true; for (var property in oAttr)//获取传入的oAttr定义的属性 { var iCur = parseFloat(_this.css(oElement, property)); property == "opacity"&& (iCur = parseInt(iCur.toFixed(2) * 100)); /*等价于if(property == "opacity"){ iCur = parseInt(iCur.toFixed(2) * 100);}*/ var iSpeed = (oAttr[property] - iCur) / 50; iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); if (iCur != oAttr[property]) { bStop = false; _this.css(oElement, property, iCur + iSpeed) } } if (bStop) { clearInterval(oElement.timer); fnCallBack && fnCallBack.apply(_this, arguments) //等价于if(fnCallBack) { fnCallBack.apply(_this, arguments)} } }, 30) } }; window.onload = function () { new ZoomPic("focus_Box"); }; 为什么addevent()不能写成this.addEvent(this.prev, "click", doPrev);

87,923

社区成员

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

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