Getting Element Path for selector

前端 未结 4 908
既然无缘
既然无缘 2020-12-16 04:59

Running into a spot of trouble and basically trying to create a variable which can be used as a selector. eg

$(\'a\').click(function(){
   var selector = $(t         


        
4条回答
  •  萌比男神i
    2020-12-16 05:30

    The request is kind of silly, since there's a much better way.

    Either assign a unique ID to the element to quickly reference it later, or if an ID is already assigned use it.

    //
    // generate a unique (enough) id for an element if necessary
    //
    function getUID(id) {
        if(window["uidCounter"]==null)
            window["uidCounter"]=0;
        return id||( (window["uidCounter"]++) + "_" + (new Date()).getTime() );
    }
    //
    // use an #ID selector
    //
    $('a').click(function(){
       var uid = getUID( $(this).attr('id') );
       $(this).attr('id', uid);
       var selector = "#" + uid;
    });
    

提交回复
热议问题