是否有“更新”函数的jQuery? [英] Is there an "Update" function for jQuery?
问题描述
如果我们将Html推送到 Dom
,它将在那里显示,但不会更新。有没有更新jQuery的功能?
<!doctype html>< html>< head>< meta charset =utf-8>< title>无标题文档< / title>< / head>< body> < div class =outer> < input type =buttonvalue =Click meid =add/>< / div><! - /.outer - >< script src =https:// ajax .googleapis.com / AJAX /库/ jquery的/ 1.12.4 / jquery.min.js>< /脚本> < / body>< / html>
当我们推送到 DOM
时,每一个答案都必须被赞赏?
您应该使用 $(document).on('click','.remove'
,而不是:
<$ ()。$'$'$'$'$'$'$'$'$'$'$'$' ).append('< div class =data>< input type =textvalue =enter/>< a href =#class =remove>删除< / a> ;< / div>);
));
$(document).on('click','.remove',function(){
alert('not working' );
});
});
jQuery监听点击文档上的事件,如果目标元素是
.remove
(例如) - 则会触发该功能。动态添加元素无关紧要 - 单击
甚至t总是在文档中,jQuery将检查目标元素(并据此采取行动)。
以下是对代码片段的更新:
pre-code = function(){$('。outer')。append('< div class =data>< input type =textvalue =enter/>< a href =#class = 删除 >删除< / A>< / DIV>'); }); $(document).on('click','.remove',function(){alert('not working');});});
<!doctype html>< html>< head>< meta charset =utf-8>< title> Untitled Document< / title>< / head>< body>< div class =outer> < input type =buttonvalue =Click meid =add/>< / div><! - /.outer - >< script src =https:// ajax .googleapis.com / AJAX /库/ jquery的/ 1.12.4 / jquery.min.js>< /脚本> < / body>< / html>
if we push Html to the Dom
, it will be display there but its not updated. is there any update function to jQuery ?
$(function(){
$('#add').on('click', function(){
$('.outer').append('<div class="data"><input type="text" value="enter" /><a href="#" class="remove">Remove</a></div>');
});
$('.remove').on('click', function(){
alert('not working');
});
});
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div class="outer">
<input type="button" value="Click me" id="add" />
</div><!-- /.outer -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</body>
</html>
Anchor link not working when we push to the DOM
, every answers must be appreciated ?
You should use the $(document).on('click', '.remove'
instead:
$(function(){
$(document).on('click', '#add', function(){
$('.outer').append('<div class="data"><input type="text" value="enter" /><a href="#" class="remove">Remove</a></div>');
});
$(document).on('click', '.remove', function(){
alert('not working');
});
});
This way jQuery listens for click
events on the document, and if the target element is .remove
(for example) - the function will be triggered. It doesn't matter if the elements are added dynamically - the click
event is always on the document, and jQuery will check the target element (and act accordingly).
Here is the update to your snippet:
$(function(){
$(document).on('click', '#add', function(){
$('.outer').append('<div class="data"><input type="text" value="enter" /><a href="#" class="remove">Remove</a></div>');
});
$(document).on('click', '.remove', function(){
alert('not working');
});
});
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div class="outer">
<input type="button" value="Click me" id="add" />
</div><!-- /.outer -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</body>
</html>
这篇关于是否有“更新”函数的jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!