开发者

javascript id select

开发者 https://www.devze.com 2023-03-25 20:13 出处:网络
I have a javascript function that someone made for 开发者_如何学JAVAme. I am kind of a noob at raw javascript. I use jquery quite often so I have been spoiled. Right now the function runs when someone

I have a javascript function that someone made for 开发者_如何学JAVAme. I am kind of a noob at raw javascript. I use jquery quite often so I have been spoiled. Right now the function runs when someone clicks anywhere on the page because the document selector is used to trigger the function. I want the function to run when a specific id is clicked. I do have jquery installed as well. Anyone have any suggestions?

addEvent('#id', 'click', function(){ does not work

addEvent(document.getElementById("id"), 'click', function(){ does not work

function addEvent(obj, type, fn) { 
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn; 
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
    obj.attachEvent( 'on'+type, obj[type+fn] ); 
  } else 
    obj.addEventListener( type, fn, false ); 
}
addEvent(document, 'click', function(){


seems to work:

http://fiddle.jshell.net/MT3ye/


The jquery way.

$("#objId").click(function(){ doSomething();});


Use document.getElementById('id') instead of document. You should have a look at this: https://developer.mozilla.org/en/The_DOM_and_JavaScript


how about using the

addEvent($('#id'), 'click', function(){


Would this work for you ...

$(document).ready(function () 
{ 
$("#element_id").click ( function(){  ... } );
...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消