javascript - Why does an error occur regarding the onclick event of an inline element?
天蓬老师
天蓬老师 2017-07-05 10:37:03
0
11
2105

An error will be reported when clicking show is not defined

Why just put the show() function outside $(function(){})?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(11)
学霸

onclick="show()" executes window.show()

typecho

The html you append is a string, and the event in onclick is show(). If the method of a specific object is not specified, it will look for the global method by default, and $(function(){}) is a closure

给我你的怀抱

Write it like this

$(function() {

            leftMenu();
            function show() {
                alert('hhhhhhhh');
            }
            function leftMenu() {
            
                var html = '<p class="show" onclick="'+show()+'">click me</dv>'
                $('#box').append(html);
            }

            
        })

Call show() directly and it will be executed immediately. If it is written as onclick="show()", $(document).ready() is executed after the document is loaded. When the page structure is completed, you click to trigger show (), at this time, you will search for show() under the window. Of course, the result is undefined. If you really want to write it in $(funciton(){}), you can do this:

$(function() {

            leftMenu();

            /*function show() {
                alert('hhhhhhhh');
            }*/
            
            function leftMenu() {
                var html = '<p class="show">click me</dv>'
                $('#box').append(html);
            }
            //做个事件委托
            $('body').on('click', $('.show'), function() {
                alert('aaaaa')
            })
            
        })
刘奇

The show function is inside an anonymous function. The onclick binding will search globally and cannot find things defined in the anonymous function

Peter_Zhu

onclick will find the custom function show() under the window object, which is window.shou(). . So it should be placed outside the $(function(){}) function. . Therefore, in the future, all custom functions should be written outside $(function(){}), and some subsequent processing and calls should be placed inside $(function(){}) to ensure that they are called after the document is loaded

黄舟

In plain language: $(function(){}) is to execute the content after the document is executed
And you added the binding after the document is loaded. Show() was not found when he executed html, please adopt it

某草草

In fact, it is a problem caused by the scope of js. onclick="show()" executes the show() method in window, but there is no such method in Window.

伊谢尔伦

Because, $(function(){}) = $.ready(), that is to say, the function show is declared after the page is loaded, but you write show() in the onclick event in the line, and the show function at this time has not yet been statement, so it will be reported as not defined

typecho

Put the definition of show method above leftMenu

扔个三星炸死你

Need to entrust~~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!