开发者

Making a selector the inner html of a link

I have a large PHP generated table of users from a database. Each username has a link and I'm trying to make it where if you click that link, the link hides and a text box becomes visible so that you ca开发者_运维百科n change the username. Each textbox has an id that matches to the link caption. When I test var x in an alert, it shows up exactly how it's supposed to, ex: '#username'. When I plug x into the selector of the show() function, however, it doesn't work.

I know that code looks somewhat confusing but really all I'm trying to do is pass $this.html() to a selector for use in the .show() function.

            $(".userLink").each(function (){
                $(this).click(function(){
                    $(this).hide();
                    var x = ("'#"+$(this).html()+"'");
                    (x).show();
                    return false;
                });
            });


$(".userLink").click(function (){
    $(this).hide();
    var x = $("'#"+$(this).html()+"'");
    x.show();
    return false;
});


Change it to

var x = $("'#"+$(this).html()+"'");
    x.show();

You were missing the $ in front of the parens.

Or you can use

var x = "'#"+$(this).html()+"'";
$(x).show();

Either way will work, you were just missing a $ where needed so it wasn't being grabbed by jQuery.

Edit

I think the other issue is you are using the html rather than the text from the link

var x = "'#"+$(this).text()+"'";
$(x).show();

I assume what you are trying to do is grab the text not all of the html, because you wouldnt have an element with the ID <div id='<a href="#">linky</a>'></div>


you don't need the quotes in the selector string.

$(".userLink").click(function(){
  $(this).hide();
  var x = "#"+$(this).text();
  // alert(x); // should be #username (without quotes)
  $(x).show();
  return false;
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜