Home > Web Front-end > JS Tutorial > body text

Solution to the problem that elements dynamically loaded by js/jq cannot be monitored

php是最好的语言
Release: 2018-07-23 12:02:50
Original
2721 people have browsed it

This article introduces the solution for the dynamically added elements of js/jq that cannot trigger binding events. If the jquery version is between 1.3-1.8, the solution for the dynamically added elements of js/jq to trigger binding events.

Please take a look at your version and get the answer:

jquery1.6 and below do not support on delegate events

jquery1.3 Live delegation events are supported up to jQuery1.8 version

jquery1.9 and later versions do not support live delegation events, but on events can replace live

jquery1.3 and below (excluding jquery1 .3), it’s time to update your jquery version. Because No solution, no solution, no solution, no solution, no solution, no solution

If the jquery version is between 1.3-1.8, the element dynamically added by js/jq triggers the solution to the binding event Method (it is not recommended to use the on event, because the on event is not supported below version 1.6, and an error will be reported)

click例子
$('element').live('click',function(){})
element可以是动态生成的元素,可以是它的类或者id
Copy after login

If the jquery version is 1.9 or higher, the live delegate event is removed, and it is done through on accomplish. Solution to the binding event triggered by elements dynamically added by js/jq

Note Note: If the page has both a low version of jq (1.3-1.8) and a high version of jq ( jquery 1.9 or above), the live delegate event will be removed by higher versions. In the end, even though the jquery version is between 1.3-1.8, if the live event is used, the page will report an error.

click例子
$('父元素').on('click', '子元素', function(){})
Copy after login

The element dynamically loaded at this time must be inside $('parent element'), otherwise the binding event will fail. In other words, the A element should be bound, but the A element is dynamically generated, so jq should get the parent element of the A element to monitor whether a click event occurs on the A element.
For example

<!DOCTYPE html>
<html>
<head>
    <title>js/jq 动态添加的元素不能触发绑定事件解决方案</title>
</head>
<script src="https://cdn.bootcss.com/jquery/1.9.0/jquery.js"></script><body>
<button>添加a标签</button>
<p class="a-Box">
    <a href="javascript:;" class="alt">My name is</a><br>
</p>
</body>
</html>
<script type="text/javascript">
    $(&#39;.a-Box&#39;).on(&#39;click&#39;, &#39;a&#39;, function(){
        alert(&#39;Jachin&#39;);
    })
    $(&#39;button&#39;).click(function(){
        $(&#39;p&#39;).append(&#39;<a href="javascript:;" class="alt">My name is</a><br>&#39;);
    })
</script>
Copy after login

This can perfectly solve the problem that dynamically loaded elements cannot be monitored.

Finally attach a version of jquery

<script src="https://cdn.bootcss.com/jquery/1.2.3/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.2.6/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.3.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.4.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.5.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.6.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.7/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.8.0/jquery-1.8.0.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.9.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.10.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/1.12.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/2.1.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/2.2.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
Copy after login

The above is the detailed content of Solution to the problem that elements dynamically loaded by js/jq cannot be monitored. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
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!