在blog的后台管理中容许为一个分类添加一个地址,可是很差添加onclick事件。想传递当前对象给一个函数,因而就将这个URL写成"Javascript:shoControlSidebar(this)",但是结果发现这并不可行,传递过去的参数是一个对象,可是却得不到任何其余信息。我想获得的是innerText,而这个this并不是指向它所在的A标签。 javascript
这是 html
<a href="Javascript:shoControlSidebar(this)">和
<a href="javascript:void(0)" onclick="shoControlSidebar(this)">不一样的地方。
当使用onclick="shoControlSidebar(this)"的时候,解释器会给他包装一个匿名函数,变成了: java
a.onclick = function anonymous() { shoControlSidebar(this); }
这个this指的就是a这个对象,而使用href的方式时,因为是一个地址,这个this就无处可指了。
ide