开发者

programatically passing an element to a function in jquery

<div id='one'>one</div>
开发者_JS百科<div id='two'>two</div>

function Red(e)
{
  $(e.target).css('color','red');  
}

// if i bind an event click to div='one' with fn "Red", function "Red" will receive e="div one", now i want to bind some event on div=two so that it send div=one to function Red.

remember: i don't want to bind any event to div=one


$('#two').bind('click', function () {
  e.target = $('#one');
  return Red(e);
});

but this is not really great, maybe you could modify your Red function to that:

function Red(el)
{
  $(el).css('color','red');  
}
$('#two').bind('click', function () {
  return Red($('#one'));
});


$('#two').click(function(){
    Red({target: $('#one')[0]});
});


If all your divs have the id attribute set, why not just do $('#one') to retrieve the div in your function?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜