0

Following this post: add class to all links that link to a certain domain with js (jquery)

I attempted to add a class to hyperlinks with jquery, but it is causing me hell. I am developing a site locally which I added this code to (as well as having jquery linked to):

<script type="text/javascript">
$("a[href*='gallery']").addClass('thumb');
</script>

with no effect on my links. I decided to strip down the page to the barest bones, see if I could identify the problem. Still no effect. As soon as I place it online, it works:

http://www.anunexpectacle.com/files/test.html

Is there something about local dev that would stop this? Why am I unable to give my links a class by jquery? Please help!

Thanks, Matt

4

1 回答 1

1

put the code inside a document.ready function so it waits for the dom to load up

$(document).ready(function(){
    // code here
});

or shortform

$(function(){
    // code here
});

Also make sure you are importing the jQuery library into your project

ex.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
于 2012-08-01T18:24:58.747 回答