开发者

How do i put this dynamic Jquery Variable in to this condition

      $(".mylink").live('click', function(){
    var linkid = $(this).attr('id');
    if($("#TBL").find('tbody > '+ #linkid ).css("display")=="inline"){
        // Do Somerthing 
     }
     });

I have to put the Dynamic variable "linkid" in to the ab开发者_JAVA百科ove if condition iam looking to have the interpreted value .Iam getting "invalid character error.how do i do this


You can just do this:

$(".mylink").live('click', function() {
  if ($(this).css("display")=="inline") {
    // Do Somerthing 
  }
});

Since IDs should be unique, you can just use this, you should already have the only element with that ID. If you have multiple elements with the same ID that's another issue that needs addressing, perhaps you should be using href="#id" for example, and $(this.hash) in the handler.


Should be as simple as:

.find("tbody > #" + linkid);

You can put the # sign directly into Javascript. Because #linkid is not valid Javascript.

To be extra careful, you might want to use a regular expression to escape CSS3 characters that are in the ID. See here for more information on what characters need to be escaped.

UPDATE

Nick's answer is correct and works for your situation. I didn't notice that you were trying to use the id of the same element that you clicked on. this refers to the element that generated the click and so you shouldn't have to grab it again via jQuery.

In general, what I wrote above will work if you want to use a variable that contains an id, in a jQuery selector string.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜