onclick中,获取不了$(this)

来源:互联网 发布:网络硬件工程师招聘 编辑:程序博客网 时间:2024/04/27 22:17

今天写项目,发现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)" onclick="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>


0 0
原创粉丝点击