Home >Web Front-end >JS Tutorial >What to do if jquery on is not available

What to do if jquery on is not available

藏色散人
藏色散人Original
2020-11-10 11:09:582798browse

The solution for jquery on being unavailable: first open the corresponding code file; then modify the writing to "$(document).on("click",".test",function(){alert(". ..");});" That's it.

What to do if jquery on is not available

The operating environment of this tutorial: windows10 system, jquery1.7. This article is applicable to all brands of computers.

Recommended: "jquery tutorial"

jQuery on() binding invalid solution

The on() method adds one or more event handlers on the selected element and sub-elements.

Since jQuery version 1.7, the on() method is the new replacement for the bind(), live(), and delegate() methods. The official website also recommends us to use this method, which simplifies the jQuery code base.

Syntax

$(selector).on(event,childSelector,data,function,map)

Parameters Description
event Required. Specifies one or more events or namespaces to be removed from the selected elements.

Multiple event values ​​separated by spaces. Must be a valid event.
childSelector Optional. Specifies that event handlers can only be added to specified child elements (and not the selector itself, such as the deprecated delegate() method).
data Optional. Specifies additional data to be passed to the function.
function Optional. Specifies a function to run when an event occurs.
map Specifies event mapping ({event:function, event:function, ...}), Contains one or more events to be added to the element, and a function to run when the event occurs.

I recently encountered that using on() was invalid at work. If the selected element and sub-elements already exist when the page is loaded, it can be used normally. The writing method is generally as follows:

$(".test").on("click",function(){
    alert("执行了");
});

If the selected element and sub-elements do not exist when the page is loaded, exists, but there is a problem if it is generated through a function. If written in the above way, no event can be bound through on().

The solution is as follows:

$(document).on("click",".test",function(){//修改成这样的写法
    alert("生成的也可以执行了!");
});

The above is the detailed content of What to do if jquery on is not available. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn