When a link is clicked I want to append something to the page. However, the link uses PHP like
(Note: This is inside an echo):
<a id = 'linkimg1' href='main.php?img1=$img1_id'> [img1 here] </a>
I have
<script>
$(document).ready(function() {
$('#linkimg1').click(function() {
$('stuff').appendTo('body')
});
});
</script>
How do I make the link still perform the jQuery script?
当您使用事件处理程序处理动态内容时,请使用jQuery.on()或jQuery.live()(不建议使用)
You have to add the selector as a parameter. If you don't then the event is bound directly which does not work for dynamic content.
<script>
$(document).ready(function() {
$(document.body).on('click', '#linkimg1' ,function(){
$('stuff').appendTo('body')
});
});
</script>
The selector is an essential part to the .on() function
.on(eventType, selector, function)
jQuery API documentation HERE
Assign it as an onClick function like so
<a id = 'linkimg1' href='main.php?img1=$img1_id' onclick="myFunction()"> [img1 here] </a>
<script>
$(document).ready(function() {
function myFunction() {
$('#linkimg1').click(function() {
$('stuff').appendTo('body')
});
}
});
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.