0

This is the code:

JS:

function f1(){
    document.getElementById('test').href="link2";
};

HTML:

<a href='link1' id='test' onclick='f1();'> Text </a>

The debugger says f1() is not defined. What could it be? The "a" tag is inside a "span" tag, maybe that?

Edit: Sorry for the JQuery thingy I added it to see what happened :P

I forgot to put the linking of the JS file, my bad:

<script type='script' href='javascript.js'> </script>
4

1 回答 1

2

你放哪儿了f1onclick在全局范围内查找函数,如果您没有在全局范围内定义该函数,则找不到。

而且$(document).getElementById('test').href="link2";也是错的,

它应该只是document.getElementById('test').href="link2";

此外,如果您使用的是 jQuery,那么最好不要使用内联onclick

$(function(){
    $('#test').click(function() {
        $(this).attr('href', 'link2');
    });
});
于 2013-09-03T01:37:13.663 回答