开发者

Click the class, hide the id

开发者 https://www.devze.com 2022-12-09 16:00 出处:网络
I want to associate a click handler with a class and then hide the element associated with the particular id that was clicked:

I want to associate a click handler with a class and then hide the element associated with the particular id that was clicked:

$(".myclass").click(function()
{
    $("#myclass_123").hide();
    return false;
});

Obviously the code above doesn't work because it doesn't calculate the "_123" part.

The ids in the class have the same name as the class that they are associated with, but they also have an underscore and a number attached to the end:

Would appreciate any he开发者_开发技巧lp identifying the id to target.


How about:

$(".myclass").click(function() {
    $(this).hide();
    return false;
});


Use $(this) to get the element clicked on

$(".myclass").click(function()
{
    $(this).hide();
    return false;
});


if the element is a anchor tag, you need a stopPropagation. otherwise you just need to hide the element.

$(".myclass").click(function(event) {
    event.stopPropagation();
    $(this).hide();
});

if you want all the other elements are seen can also be shown in this way.

$(".myclass").click(function(event) {
    event.stopPropagation();
    $(".myclass").show();
    $(this).hide();
});
0

精彩评论

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