开发者

remembering what $(this) is

I have the following script which changes a jquery made object's color to blue:

$(".objects_list").live('click', function(event)
{
    $(this).css("color", "blue");
});

How do I remember 开发者_如何学Pythonwhat $(this) is so I can change the color again but from a different function or from an event of a different object?


Instead of a global variable, you can use jQuery's data() method to associate information with the document itself:

$(".objects_list").live('click', function(event) {
    $(this).css("color", "blue");
    $(document).data("yourObjectKey", $(this));
});

Then you can easily get that information later:

$("otherSelector").click(function() {
    var yourObject = $(document).data("yourObjectKey");
    if (yourObject != null) {
        yourObject.css("color", "red");
    }
});

EDIT: If the element is destroyed and recreated between the two events, that method won't work. In that case, you can store the element's id instead of a reference to the element itself:

$(".objects_list").live('click', function(event) {
    $(this).css("color", "blue");
    $(document).data("yourObjectKey", this.id);
});

Then:

$("otherSelector").click(function() {
    var yourObjectId = $(document).data("yourObjectKey");
    if (yourObjectId != null) {
        $("#" + yourObjectId).css("color", "red");
    }
});


set an out of scope variable to be the object.

var thisObj;
$(".objects_list").live('click', function(event)
{
    thisObj = $(this);
    $(this).css("color", "blue");
});


var lastObj = null;
$(".objects_list").live('click', function(event)
{
    $(this).css("color", "blue");
    lastObj = $(this);
});

some_other_function()
{
    if ( lastObj != null )
        lastObj.css("color", "red");
}


You could add an id attribute to indicate the "clicked" item, then other functions could select on that id. No need for global variables of any kind:

$(".objects_list").live('click', function(event)
{ 
    $(this).css("color", "blue");
    $('#objects_list_clicked').removeAttr("id");
    $(this).attr("id", "objects_list_clicked");
});


Could you attach an event listener to "$(this)" to listen for when the color should be changed back?


Not the cleanest solution, but you could assign it to a global variable:

myElement = $(this);


One option would just be to save $(this) in a variable:

var saved;
$(".objects_list").live('click', function(event)
{
    $(this).css("color", "blue");
    saved = $(this);
});


Try it Differently:

$(".objects_list").live('click', function(event)
{
    var $this = $(this);
    $(".objects_list").removeAttr('selected');
    $this.attr('selected','selected');
        $this.css("color", "blue");
});

Now your selection is not stroed as javascript varible but in DOM. You can change the setting anytime you want and from anywhere using followign code

if($(".objects_list").attr('selected').length != 0){
    var selectedTab = $(".objects_list").attr('selected');
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜