开发者

select the Div without specific content

Q:

I have the following case :

Div contains a link , i wanna to just select the div without the link,i mean ,when clicking on the div i wanna specific action differs from clicking the link.through some JQuery.

the structure i work on is:(by firebug)

<div class ="rsAptContent"> sql <a class = "rsAptDelete" href = "#" style ="visibility: hidden;">Delete</a> </div>

the JQuery code:

<script type="text/javascript">

    $(document).ready(function() {


    $(".rsAptContent").click(function(e) {
            ShowDialog(true);
            e.preventDefault();
        });

    });

    function ShowDialog(modal) {
        $("#overlay").show();
        $("#dialog").fadeIn(300);

        if (modal) {
            $("#overlay").unbind("click");
        }
        else {
            $("#overlay").click(function(e) {
                HideDialog();
            });
        }
    }

    function HideDialog() {
        $("#overlay").hide();
        $("#dialog").fadeOut(300);
    } 

</script>`

when i click on the link ,i don't want to execute the Jquery code , how to select the div without the开发者_StackOverflow社区 link in.

thanks in advance


Are you looking for something like the stopPropagation() code?

$(".rsAptContent").click(function(e) {
            e.stopPropagation();
            ShowDialog(true);
            return false;
        });

    });

That should stop the link from executing.

http://api.jquery.com/event.stopPropagation/

Edit: Distinguish between clicking the link and clicking on any part of the content except the link

    $(".rsAptContent").click(function(e) {
                var $target = $(e.target);
                if($target.is(a){
                  // It's the link.
                }else{
                  // else it's not
                }
            });
        });


Check for the clicked target element than perform action

to get info about which element is click use below script

function whichElement(event){
    var tname
    tname=event.srcElement.tagName
    alert("You clicked on a " + tname + " element.")
}


Try this:

$(".rsAptContent").click(function(e) {
        if($(e.target).hasClass('rsAptDelete')) return false;
        ShowDialog(true);
        e.preventDefault();
    });
});

If the target is the link the event is cancelled;


If you already have a click handler on the delete link, then just stop the event propagation there by using stopPropagation().

$(".rsAptDelete").click(function(e) {
    e.stopPropagation();
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜