亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

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