문제

I have a mustache template in my javascript which gets put into my html

This is made up of H2 tags and ul/li tags

I have this at the bottom of my javascript

    function vara(){
        alert('f')
    }

    $('h2').click(vara)

However this does not work when I click on H2 tags created by the mustache template. How can I remedy this?

도움이 되었습니까?

해결책 2

Your content is being added by mustache after run-time, and since your handlers are bound at run-time, they have no idea what to bind to. The correct way is to use .on and bind the click event to the container of your appended content.

$(document).on("click", "h2", vara);

다른 팁

By waiting for a document to be ready.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top