$("p").on('click',function(){ console.log('点击了'+$(this).data('name')); }) 这段代码如果用原生的写应该怎么写
ringa_lee
楼上的,我权限不够点不了反对,开玩笑
var p = document.querySelectorAll('p'); [].forEach.call(p, function(item) { item.addEventListener('click', function() { console.log('点击了' + this.dataset.name); }); });
//兼容性解决 function addevent(obj,event,func){ if(obj.attachEvent){ obj.attachEvent("on"+event,func) } else{ obj.addEventListener(event,func,false); } } var ap=document.getElementsByTagName('p'); for(var i=0;i<ap.length;i++){ addevent(ap[i],'click',function(){ console.log(this.data('name')); }) }
楼上的,我权限不够点不了反对,麻烦至少也是
var p=document.getElementsByTagName("p")[0];//看你取的第几个 p.addEventListener('click',function(){ console.log('点击了'+$(this).data('name'));//如果要绑定所有p 要一个个addEventListener ,或者用事 //件冒泡什么的,看需求 })
楼上的,我权限不够点不了反对,开玩笑
楼上的,我权限不够点不了反对,麻烦至少也是