开发者

How to access the element using "this"

I need to access a SPAN tag 开发者_开发问答with in every DIV tag

so i used this following code

    $("DIV").click(function(){      
    $(this + "SPAN").show();
});

Is the above code is correct? Its not working for me! Its showing nothing too..

Please help me

Thanks,

Praveen J


You can use .find() for getting an element inside another, like this:

$("div").click(function(){      
  $(this).find("span").show();
});

As a general rule, to get anything relative to this, you're typically going to start with $(this) and some combination of the tree traversal functions to move around.


For actual code, based on comments below:
If your code looks like this:

<fieldset>
  <legend>Link</legend>
  <span>CHECK</span>
</fieldset> 

Then .find() won't work since on $("legend") selector because the <span> isn't inside the <legend> it's a sibling, so use .siblings() (optionally with a selector) like this:

$("legend").click(function(){ 
  $(this).siblings("span").show(); 
});​

You can give it a try here


You can use the find function to do that. (Also, using lowercase for selectors is preferred.)

$("div").click(function(){      
    $(this).find("span").show();
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜