简体   繁体   中英

Stop some links (href containing certain string) from working

I'm attempting to stop some links on my site from working.

Here is my code:

Links:

<a href='http://mysite.home.com'>Home</a><br>
<a href='http://mysite.home.com/nextpage'>Next Page</a><br>
<a href='http://mysite.string.home.com'>Home 2</a><br>
<a href='http://mysite.string.home.com/otherpage'>Other Page</a>

Jquery:

<script>
    $("a[href*='.string.']").click(function(e) {
        e.preventDefault();
        alert('You cannot use this link');
    });
</script>

The goal is to stop the last two links (with href containing '.string.') from working and instead display the alert. However, this code isn't working and the links still work. What am I doing wrong?

The strange thing is the code works fine here: http://jsfiddle.net/TzUN3/364/ but not on my site. I have the script and links both in the <body> of my page, script after the links. Is this correct?

Thanks.

Forgot to add

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

to my page. Feel like an idiot.

Thank you all.

$(function() {
    // Jquery is ready
    // *= is contains
    $("a[href*='.string.']").click(function(e) {
        e.preventDefault();
        alert('You cannot use this link');
    }); 

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM