超链接onclick功能无法正常工作

use*_*619 0 html jquery

我需要处理超链接上的onlick函数,但它不起作用..

HTML代码: -

<a href="#" onclick="updateParent()" id="2" value="2">CLICK HERE</a>
Run Code Online (Sandbox Code Playgroud)

jQuery的: -

$(document).ready(function(){

    function updateParent(control) {
                    alert('Hi')
                }

})
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/sbnBy/1/

Roc*_*mat 6

updateParent只存在于.ready()函数内部.它需要全球化onclick才能发挥作用.

此外,您不应该使用内联事件.你有jQuery,使用它.

<a href="#" id="2" value="2">CLICK HERE</a>

<script>
    $(document).ready(function(){
        $('#2').click(function(e){
            // preventDefault... it prevents the "default" action of the tag
            // In this case, it would try to nagivate to `#`.  Which would
            // scroll the page to the top.
            e.preventDefault();

            alert('hi');
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)