当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery switch statement and scope issue

问题描述:

I have this working script (i know its incomplete):

function rsvpNew(selector,function_url) {

$(selector).livequery('click',function(){

var select = $(selector).attr('rel');

var event_id = $(this).parents('ul.event-options').attr('rel');

element = this;

switch(select) {

case "attending":

$.ajax({

type: "POST",

url: "/events/set_member/"+function_url,

data: "event_id="+event_id,

beforeSend: function() {

$("<span class='notice'>Saving...</span>").prependTo('body');

},

complete: function() {

$('span.notice').fadeOut(500);

$(element).closest('span.rsvp-status').html("I'm Attending &ndash; <a href='javascript:;' class='remove' rel='remove'>Remove</a>");

}

});

break;

Now the line

$(element).closest('span.rsvp-status').html("I'm Attending &ndash; <a href='javascript:;' class='remove' rel='remove'>Remove</a>");

Is working, but when I do something similar to get the parent ul of this li, it doesn't work.

Any ideas?

网友答案:

but when I do something similar to get the parent ul of this li, it doesn't work. Any ideas?

To get the parent of something you can use a few options:

$('li').parent('ul'); //will return your ul

Gets the direct parent of an element.

Another way is to use closest:

$('li').closest('ul'); //should also return your ul

Gets a set of elements containing the closest parent element that matches the specified selector. Especially useful for event delegation.

分享给朋友:
您可能感兴趣的文章:
随机阅读: