开发者

Jquery get a reference to the inital node

I have this line:

$('.faq_answer').prev().append(finalRender.cloneNode(true)).click(function () {toggle(this)});

I need to replace the this keyword with a reference to the node in $('.faq_answer.') that is currently being operated on.

I have checked the Jquery documentat开发者_Python百科ion and it doesn't seem to cover this situation.


this in the context of any function, is scoped to that function. Therefore, you should first get a reference to .fag_answer, then you can use it.

 var faq = $('.faq_answer');
 faq.prev().append(finalRender.clonNode(true)).click(function(){toggle(faq);});


As described by Nicola above, you can create a reference to the selector and then use it within your click handler:

var faq = $('.faq_answer');
faq.prev().append(finalRender.cloneNode(true)).click(function () {toggle(faq)});

You might also want to create a closure around it.


You could do:

     click(function (event) {
      //event.target is the element that has been clicked and you could use this instead of click
      });

If you want a refernce to the selctor (which i didn't think you wanted so i deleted my answer), you can simply do this:

    var faq = $('.faq_answer')


OK I have found a way of doing this:

var faq = $('.faq_answer');
faq.prev().append(finalRender.cloneNode(true)).click(function () {toggle($(this).siblings())});

Gives me all the siblings of the current faq dom object.

Then in my toggle function I added:

element = element.get(0);

Which appears to get me the dom element I want to operate on. For the moment I'm assuming it is robust, but really I don't understand why it should work like that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜