今天写项目,发现onclick中,$(this)默认绑定的是window对象,而不是当前点击对象。试了一下,onclick中,必须带this,在function内,才能获取当前对象。
原因:在html4中,onclick是全局时间属性。参考:http://www.w3school.com.cn/tags/html_ref_eventattributes.asp
所以,在事件中,添加this,返回触发事件的html元素
<a href="javascript:void(0)" οnclick="openNewsList(url,this)" class="index-news-list">链接</a>
<script type="text/javascript">
function openNewsList(url, obj) {
console.dir(obj);
$(obj).removeClass("index-news-list");
window.open(url);
}
</script>