简体   繁体   中英

Creating a same-page event with JQuery

I am trying to make jquery trigger an event when an anchor link has the same URL as the current page. Eventually this will add a class to the anchor link and let me style it differently with CSS but for now I just want it to show an alert box. Code below doesn't seem to work:

<script type="text/javascript">
$(document).ready(function() {

var buildurl = window.location.href;

if($('a').attr('href') = buildurl){

    alert("Done");

}

});
</script>

Can anybody shine a light on why this isn't working? Or a best practice for similar using JQuery?

Link is: http://www.otahboy.com/shop/index.php?route=information/information&information_id=4

Cheers,

Jack

<script type="text/javascript"> 

$(document).ready(function() {

var buildurl = window.location.href;

if($('a').attr('href') == buildurl){

    alert("Done");

}

}); 

</script>

use equality operator ==

if($('a').attr('href') == buildurl){

    alert("Done");
$('a').attr("color","Red");
}

You have an assignment in your if condition. You need to add one = to it.

<script type="text/javascript">
$(document).ready(function() {

var buildurl = window.location.href;

if($('a').attr('href') == buildurl){

    alert("Done");

}

});

</script>

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