jQuery Appended elements with href and javascript doesn#39;t work(带有href和javascript的jQuery附加元素不起作用)
问题描述
So I'm appending some elements with jQuery that contain href that calls another script when the anchor is clicked, but doesn't work; here's an example:
$(".un_comentario").hover(
function () {
$("#dedos_voto", this).append($("<span id="commentup" class="vote_up"><a href="javascript:;" ><img src="L21pbWcvY29tbWVudC9jc3RodW1idXAucG5n" /></a><?php echo $utilchido;?></span>"));
}
);
So as you can see the anchor has an href that calls another script, here's the other script:
$(function(){
$("span.vote_up").click(function(){
//get the id
the_id = $("div.un_comentario").attr('id');
$.ajax({
type: "POST",
data: "action=vote_up&id="+$("div.un_comentario").attr("id"),
url: "votacomment.php",
success: function(){alert("Gracias, el voto se ha agregado.");
}
});
});
});
So the script only works if the elements are in the html markup, but not if the elements are appended with jQuery.
Any suggestion?
you are dynamically adding the href links to the DOM use .live()
$("span.vote_up").live('click',function(){
//rest of your logic
});
这篇关于带有href和javascript的jQuery附加元素不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有href和javascript的jQuery附加元素不起作用


- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01