Pergunta

I have found this jsfiddle code on Jquery http://jsfiddle.net/anderskitson/AtqHm/ it works fine on jsfiddle, however when I try and implement it in a html file it doesn't work. I have it hosted at http://anderskitson.ca/tests/javascript/clearOnHover.html I am not sure why its not working it is pretty basic setup. Any help would be greatly appreciated.

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Rat Recipes</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
    </head>
    <body>
        <div id="form">
    <form method="post">
    <input type="text" class="class-11" value="Enter Choice #11">
    <input type="text" class="class-21" value="Enter Choice #21">​
    <input name="submit" type="submit" id="submit" class="submit button" value="Send feedback" />
    </form>
         <script>
        $(document).ready(function() {
        var default_val = '';
        $('input[class^="class-"]').focus(function() {
            if($(this).val() == $(this).data('default_val') || !$(this).data('default_val')) {
                $(this).data('default_val', $(this).val());
                $(this).val('');
            }
        });

        $('input[class^="class-"]').blur(function() {
            if ($(this).val() == '') $(this).val($(this).data('default_val'));
        });
    });​
        </script>
    </body>
</html>
Foi útil?

Solução

It looks like you have some unrecognized character at the end of the ready() function that isn't appearing in the snippet above. When I copy/paste your code into Notepad++ I see a "?" character. Removing that causes the code to function properly

Outras dicas

I'm getting an error in Chrome on line 29, which suggests to me that for some reason browser(s) in the wild are being less forgiving about your lack of curly braces on the final if statement. Try:

if ($(this).val() == '') { $(this).val($(this).data('default_val')); }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top